In the following questions, You can pick your own timer, 8-bit or 16-bit mode, a
ID: 3552491 • Letter: I
Question
In the following questions, You can pick your own timer, 8-bit or 16-bit mode, and the prescaling ratio. You can assume XTAL = 10M Hz or at the frequency you specify.
Write a C program to generate a frequency of
200 Hz on PORTA pin 3. You do not have to write all the code, just specify which example(s) of the Mazidi book you modify from, with the page number. Show the new contents of Timer configuration register, Timer High (say TMR0H), Timer Low register if applicable. Also show how you calculate. (in other words, the original code may have T0CON = 0x0; and your new code may have T0CON = 0x88; the original code may show TMR0H = 0xFF; and your new code may show TMR0H = 0x ED; Explain and show how you derive these)
200 Hz on PORTA pin 3 when a switch SW = 0 and 400 Hz on PORTA pin 4 when SW = 1 for the same switch. Assume this switch SW connects to PORTA pin 0. The code of part (b) can come from (a) with suitable modification.
With 2 switches SW1 and SW2:
200 Hz on PORTA pin 3 when SW1 = 0, SW2= 0;
400 Hz on PORTA pin 4 when SW1 = 1, SW2 = 0;
800 HZ on PORTA pin 5 when SW1 = 0, SW2 = 1; and
1600 HZ on PORTA pin 6 when SW1 = 1, SW2 = 1.
Here SW1 is connected to PORTA pin 0 and SW2 is connected to PORTA pin1.
Write a C program with a 200 Hz external clock fed into pin T1CKI (not T0CKI) to display the seconds and minutes on PORTB and PORTD. You can pick your timer as in 8 bit or 16 bit mode. Show which example can be modified from and how the code is modified.
Explanation / Answer
1
1............................
200 HZ = 1/200sec = 0.005sec
if we set prescalar to 1
if the timer0 increases C counts
time elapsed = C/(XTAL/4) = 4*C/XTAL
XTAL=10Mhz
time = 4*C/10000000 =0.005
4C= 50000
C=12500
To increase 12500 counts value in timer0 = 65535-12500 =53035 = 0XCF2B
Hence Code would be
T0CON=0x08;
TMR0H=0xCF;
TMR0L=0x2B;
2........................
400 HZ = 1/400= 0.0025sec
if we set prescalar to 1
if the timer0 increases C counts
time elapsed = C/(XTAL/4) = 4*C/XTAL
XTAL=10Mhz
time = 4*C/10000000 =0.0025
4C= 25000
C=6250
To increase 6250 counts value in timer0 = 65535-6250=59285= 0XE795
//please include the releavant pic files
void main (void)
{
TRISAbits.RA0=1;//input
TRISAbits.RA3=0;//output
TRISAbits.RA4=0;//output
while(1)
{
if(PORTA.RA0==0)
{
T0CON=0x08;
TMR0H=0xF3;
TMR0L=0xCA;
T0CONbits.TMR0ON=1;
LATA3 =~PORTAbits.RA3; // TOGGLE PORTA PIN3. REMEMBER WRITE TO LATCH READ FROM PORT
while(INTCONbits.TMR0IF==0);// wait for timer to overflow
T0CONbits.TMR0ON=0; // stop timer
INTCONbits.TMR0IF=0; // clear overflow flag
}
if(PORTA.RA0==1)
{
T0CON=0x08;
TMR0H=0xE7;
TMR0L=0x95;
T0CONbits.TMR0ON=1;
LATA4 =~PORTAbits.RA4; // TOGGLE PORTA PIN3. REMEMBER WRITE TO LATCH READ FROM PORT
while(INTCONbits.TMR0IF==0);// wait for timer to overflow
T0CONbits.TMR0ON=0; // stop timer
INTCONbits.TMR0IF=0; // clear overflow flag
}
}
3.........................................
800HZ = 1/800= 0.00125sec
if we set prescalar to 1
if the timer0 increases C counts
time elapsed = C/(XTAL/4) = 4*C/XTAL
XTAL=10Mhz
time = 4*C/10000000 =0.00125
4C= 12500
C=3125
To increase 3125 counts value in timer0 = 65535-3125=62410= 0xF3CA
1600HZ = 1/1600= 0.000625sec
if we set prescalar to 1
if the timer0 increases C counts
time elapsed = C/(XTAL/4) = 4*C/XTAL
XTAL=10Mhz
time = 4*C/10000000 =0.000625
4C= 6250
C=1563
To increase 1563 counts value in timer0 = 65535-1563=63972= 0xF9E4
//please include the releavant pic files
void main (void)
{
TRISAbits.RA0=1;//input
TRISAbits.RA1=1;//input
TRISAbits.RA3=0;//output
TRISAbits.RA4=0;//output
TRISAbits.RA5=0;//output
TRISAbits.RA6=0;//output
while(1)
{
if(PORTA.RA0==0 && PORTA.RA1==0)
{
T0CON=0x08;
TMR0H=0xCF;
TMR0L=0x2B;
T0CONbits.TMR0ON=1;
LATA3 =~PORTAbits.RA3; // TOGGLE PORTA PIN3. REMEMBER WRITE TO LATCH READ FROM PORT
while(INTCONbits.TMR0IF==0);// wait for timer to overflow
T0CONbits.TMR0ON=0; // stop timer
INTCONbits.TMR0IF=0; // clear overflow flag
}
if(PORTA.RA0==1 && PORTA.RA1==0)
{
T0CON=0x08;
TMR0H=0xE7;
TMR0L=0x95;
T0CONbits.TMR0ON=1;
LATA4 =~PORTAbits.RA4; // TOGGLE PORTA PIN3. REMEMBER WRITE TO LATCH READ FROM PORT
while(INTCONbits.TMR0IF==0);// wait for timer to overflow
T0CONbits.TMR0ON=0; // stop timer
INTCONbits.TMR0IF=0; // clear overflow flag
}
if(PORTA.RA0==0 && PORTA.RA1==1)
{
T0CON=0x08;
TMR0H=0xF3;
TMR0L=0xCA;
T0CONbits.TMR0ON=1;
LATA5 =~PORTAbits.RA5; // TOGGLE PORTA PIN3. REMEMBER WRITE TO LATCH READ FROM PORT
while(INTCONbits.TMR0IF==0);// wait for timer to overflow
T0CONbits.TMR0ON=0; // stop timer
INTCONbits.TMR0IF=0; // clear overflow flag
}
if(PORTA.RA0==1 && PORTA.RA1==1)
{
T0CON=0x08;
TMR0H=0xF9;
TMR0L=0xE4;
T0CONbits.TMR0ON=1;
LATA6 =~PORTAbits.RA6; // TOGGLE PORTA PIN3. REMEMBER WRITE TO LATCH READ FROM PORT
while(INTCONbits.TMR0IF==0);// wait for timer to overflow
T0CONbits.TMR0ON=0; // stop timer
INTCONbits.TMR0IF=0; // clear overflow flag
}
}
2......................................................................................
if the timer0 increases C counts
time elapsed = C/(200) = 1sec
C=200
To increase 200 counts value in timer0 =255-200 =55 = 0x37 in hex
//please include the releavant pic files
void myTimer(unsigned int);
void main()
{
unsigned int val=0;
TRISB=0;// output
TRISD=0; // output
T0CON=0x68;
TMR0L=0x37;
while(1)
{
T0CONbits.TMR0ON=1;
while(INTCONbits.TMR0IF==0);// wait for timer to overflow
T0CONbits.TMR0ON=0; // stop timer
INTCONbits.TMR0IF=0; // clear overflow flag
val++
myTimer(val);
}
}
void Timer(unsigned int val)
{
unsigned char sec,min;
min=val/60;
sec=val%60;
PORTB=sec;
PORTC=min;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.