Home › Forums › Microcontrollers › PIC Microcontroller › PIC Microcontroller Non-Blocking Delay Implementation › Reply To: PIC Microcontroller Non-Blocking Delay Implementation
February 2, 2016 at 2:03 pm
#12105
Participant
This is how I have changed my code and the result is still the same :
char uart_rd;
char cnt, cnt1;
void interrupt ()
{
if (TMR0IF_bit)
{
cnt++; // increment counters
cnt1++;
TMR0IF_bit = 0; // clear TMR0IF
TMR0L = 0xDC; // set TMR0 for aproximetly 1 sec
TMR0H = 0x0B;
}
}
void one_min_delay ()
{
TRISB = 0; // PORTB is output
TMR0H = 0x0B;
TMR0L = 0xDC; // Timer0 initial value
T0CON = 0x84; // Set TMR0 to 8bit mode and prescaler to 256
GIE_bit = 1; // Enable global interrupt
TMR0IE_bit = 1; // Enable Timer0 interrupt
cnt = 0; // Initialize cnt
do
{
if (cnt >= 60)
{
LATB = 0x00; // turnoff PORTB LEDs
cnt = 0; // Reset cnt
}
}while(1);
}
void two_min_delay ()
{
TRISB = 0; // PORTB is output
TMR0H = 0x0B;
TMR0L = 0xDC; // Timer0 initial value
T0CON = 0x84; // Set TMR0 to 8bit mode and prescaler to 256
GIE_bit = 1; // Enable global interrupt
TMR0IE_bit = 1; // Enable Timer0 interrupt
cnt1 = 0; // Initialize cnt1
do
{
if (cnt1 >= 120)
{
LATB = 0x00; // turnoff PORTB LEDs
cnt1 = 0; // Reset cnt
}
}while(1);
}
void main()
{
UART1_Init(9600); // Initialize UART module at 9600 bps
Delay_ms(100); // Wait for UART module to stabilize
TRISB = 0x00;
while (1)
{
if (UART1_Data_Ready())
{ // If data is received,
uart_rd = UART1_Read();
}
if (uart_rd=='A')
{
LATB = 0x01;
one_min_delay();
}
else if (uart_rd == 'B')
{
PORTB = 0x02;
two_min_delay ();
}
uart_rd ='\0';
}
}
