Now I am learning use time0 to time delay while rotating rhe demo board LEDs Thi
ID: 3623448 • Letter: N
Question
Now I am learning use time0 to time delay while rotating rhe demo board LEDsThis is source code
#include "p18f46k20.h"
#include "Timer.h"
#pragma udata
#pragma code
void main (void)
{
LEDDirections Direction = LEFT2RIGHT;
BOOL SwitchPressed = FALSE;
LED_Display = 1;
// Init I/O
TRISD = 0b00000000; // PORTD bits 7:0 are all outputs (0)
INTCON2bits.RBPU = 0; // enable PORTB internal pullups
WPUBbits.WPUB0 = 1; // enable pull up on RB0
ANSELH = 0x00; // AN8-12 are digital inputs (AN12 on RB0)
TRISBbits.TRISB0 = 1; // PORTB bit 0 (connected to switch) is input (1)
// Init Timer
INTCONbits.TMR0IF = 0; // clear roll-over interrupt flag
T0CON = 0b00001000; // no prescale - increments every instruction clock
//T0CON = 0b00000001; // prescale 1:4 - four times the delay.
TMR0H = 0; // clear timer - always write upper byte first
TMR0L = 0;
T0CONbits.TMR0ON = 1; // start timer
while (1)
{
if (Direction == LEFT2RIGHT)
{
LED_Display <<= 1; // rotate display by 1 from 0 to 7
if (LED_Display == 0)
LED_Display = 1; // rotated bit out, so set bit 0
}
if (Direction == RIGHT2LEFT)
{
LED_Display >>= 1; // rotate display by 1 from 7 to 0
if (LED_Display == 0)
LED_Display = 0x80; // rotated bit out, so set bit 7
}
LATD = LED_Display; // output LED_Display value to PORTD LEDs
do
{ // poll the switch while waiting for the timer to roll over.
if (Switch_Pin == 1)
{ // look for switch released.
SwitchPressed = FALSE;
}
else if (SwitchPressed == FALSE) // && (Switch_Pin == 0) due to if-else
{ // switch was just pressed
SwitchPressed = TRUE;
// change direction
if (Direction == LEFT2RIGHT)
Direction = RIGHT2LEFT;
else
Direction = LEFT2RIGHT;
}
} while (INTCONbits.TMR0IF == 0);
// Timer expired
INTCONbits.TMR0IF = 0; // Reset Timer flag
}
}
what is the mean of code:'LED_DISPLAY=0X80'
If i want to rotate every 0.52 second, how to do it
Please give me the answers
Thank you very much !
Explanation / Answer
Dear, ‘LED_DISPLAY = 0X80’ LED Display used in Timer0 interrupt function, In main program while() loop updates PORTD register with LED_DISPLAY Prog.c uses a single set bit in LED_DISPLAY variable written in LATD and shifted each time the display can update. This bit will be finally shifted out of the most significant. 0x80 sets bit 7 -- 111. ------------------------------------------------------------------------------------------------- We know that Timer0_Init() sets the TMR0 prescalar to 1:4 and oscillator 1MHZ Timer0 will count at 4*instruction rate or (4*1/(FOSC/4)) = 4*1/(1MHZ/4) = 4*1/250KHZ = 16us. The number of counts until overflow occurs is 0x10000-(start count) Start count means the value written to TMR0 The total delay is number of counts the time of count rate For 0.52us delay = 32512*16us = 0x7F00*16us = (0x10000-0x8100)*16us =0x81 is the delay LED_DISPLAY = 0x81
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.