Tutorial 10_C_Timer Programmig(LO3)
Tutorial 10_C_Timer Programmig(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
// 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)
#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
}
}
}