0% found this document useful (0 votes)
2 views2 pages

Tutorial 10_C_Timer Programmig(LO3)

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
2 views2 pages

Tutorial 10_C_Timer Programmig(LO3)

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 2

Tutorial 08 C_Timer Programming (LO3)

A microcontroller based system requires an input clock signal to generate a 10Hz pulse width
modulation (PWM) signal. Write a C code segment based on PIC16F microcontroller to
accommodate these requirements.
You may assume the following:
 Timer/Counters should be used to generate the input clock signals.
 You are free to choose any clock frequencies and timer modes for the microcontroller.
 Use C as the programming language (with comments).
 All port pins may be used as general purpose digital I/O.
 State any assumptions you make in your answer.

Solution
Calculation varies based on the cristol, prescaler and TMRO value used

 Here, fclk = 20MHz (You can put your board’s fclk)


 Prescaler = 256 (It is based on PS0 – PS2 bits in OPTION_REG)
 TMR0 = 0 (our TMR0’s value will be 0)
 Desire Delay (Tout = 1 second) So Fout = 1 (Tout = 1/Fout)
 Apply this values to that above formula.
 Count = 20,000,000 / (4*256*256*1)
 Count = 76.29394 (approximately 76).

// PIC16F877A Configuration Bit Settings

// 'C' source line config statements

// CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR disabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming
Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM
code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection
off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection
off)

// #pragma config statements should precede project file includes.


// Use project enums instead of #define for ON and OFF.
#define _XTAL_FREQ 20000000

#include <xc.h>
//#include<htc.h>

int Count=0;
void main(void)
{
TMR0=0; //TMR0 Initiation
T0CS=0; //Choosing to work with internal clk
T0SE=0; //Reacting on Low2High edge
PSA=0; //Choosing to work with a Prescaler
PS0=1;
PS1=1; //Prescaler value divides 256
PS2=1;
TRISB=0x00;
PORTB=0x00;
while(1)
{
while(!TMR0IF); //Stays here 256 times and then T0IF=1
TMR0IF=0; //Resetting the overflow flag
Count++; //Increasing by 1
if(Count==76)
{ PORTB = ~PORTB;
Count=0; //when count reaches 15 - Resetting to 0
}
}
}

You might also like