I am writing code to display voltage data on the LCD on a PIC16F877A. To observe the values on the display, it needs a delay but when I use the delay function, it gets stuck. When I change the pot value, it will not show during the observation delay. So I need assistance and guidance to introduce delay. When I change the value of the pot, I should change it during a 5 second delay. I share my code below:
// Lcd pinout settings
sbit LED1 at RB4_bit;
sbit LCD_RS at RD2_bit;
sbit LCD_EN at RD3_bit;
sbit LCD_D4 at RD4_bit;
sbit LCD_D5 at RD5_bit;
sbit LCD_D6 at RD6_bit;
sbit LCD_D7 at RD7_bit;
//sbit SW at RB1_bit;
// Pin direction
sbit LCD_RS_Direction at TRISD2_bit;
sbit LCD_EN_Direction at TRISD3_bit;
sbit LCD_D4_Direction at TRISD4_bit;
sbit LCD_D5_Direction at TRISD5_bit;
sbit LCD_D6_Direction at TRISD6_bit;
sbit LCD_D7_Direction at TRISD7_bit;
unsigned short count,pls;
unsigned char ch,bh;
long tlong,blong;
void adc1_config();
void adc2_config();
int adc1_prcs();
int adc2_prcs();
void main()
{
adc1_config();
adc2_config();
ADC_Init();
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
adc1_prcs();
adc2_prcs();
Lcd_Out(1,1,"VAC-IN : ");
Lcd_Out(2,1,"VAC-OUT: ");
delay_ms(5000);
// here i want delay to adjust input voltage and calibrate.
// delay does not work here bcoz adc value stuck due to delay function.
// suugest proven technique to avoid delay.
TRISB.F4 = 0;
while(1)
{
Lcd_Out(1,1,"info page");
Lcd_Out(2,4,"123456789");
delay_ms(5000);
//same scenario occur here
Lcd_Cmd(_LCD_CLEAR);
adc1_prcs();
adc2_prcs();
Lcd_Out(1,1,"VAC-IN : ");
Lcd_Out(2,1,"VAC-OUT: ");
delay_ms(5000);
Lcd_Cmd(_LCD_CLEAR);
// and here also
}
}
I tried using the mikroC compiler.
Your code needs to avoid delay altogether - during a delay without multi-threading support, the CPU stalls doing no useful work (unless performed in interrupt handlers). One solution to that (short of an RTOS) is rather than delay, simply poll a free-running counter of elapsed_time and perform actions whatever they become " due ".
I am no PIC developer and I have synthesised the following from information on various sites - it may not be correct and most of it was for Microchip's XC8 compiler and may need some changes for MikroC - I think they have a different naming convention for peripheral register access structures.
So given the following 1ms tick implementation using TIMER 0:
#define FOSC_FREQ 4000000 //4MHz
// 1/1000 seconds 24=56 prescaler
#define TIMER_1MS (256 - FOSC_FREQ / (1024 * 1000) )
static volatile uint32_t ms_tick = 0 ;
uint32_t initMsTick()
{
OPTION_REGbits.PSA = 0;
OPTION_REGbits.PS = 0b111; //Set the prescaler to 1:256
OPTION_REGbits.T0CS = 0;
INTCONbits.T0IF = 0; //Clear the Timer 0 interrupt flag
TMR0 = TIMER_1MS; //Load counter for 1ms tick
INTCONbits.T0IE = 1; //Enable the Timer 0 interrupt
INTCONbits.GIE = 1; //Set the Global Interrupt Enable
}
uint32_t getMsTick()
{
uint32_t now = ms_tick ;
while( now != ms_tick )
{
now = ms_tick ;
}
return now ;
}
void interrupt()
{
if( INTCONbits.T0IF)
{
INTCONbits.T0IF = TIMER_1MS ;
ms_tick++ ;
}
}
you might then create a timer interface thus:
#include <stdbool.h>
typedef struct
{
uint32_t ref ;
uinr32_t period ;
} timer_t ;
void startTimer( timet_t* timer, uint32_t period )
{
timet->ref = getMsTick() ;
timer->period = period ;
}
bool hasTimerExpired( timer_t* timer )
{
return (getMsTick() - timer->ref) >= timer->period ;
}
Then in your application for example:
adc1_config();
adc2_config();
ADC_Init();
Lcd_Init();
Lcd_Cmd(_LCD_CURSOR_OFF);
TRISB.F4 = 0;
timer_t display_time ;
for(;;)
{
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,"VAC-IN : ");
Lcd_Out(2,1,"VAC-OUT: ");
startTimer( &display_time, 4000u ) ;
while( !hasTimerExpired( &display_time ) )
{
adc1_prcs();
adc2_prcs();
}
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,"info page");
Lcd_Out(2,4,"123456789");
timer_t display_time ;
startTimer( &display_time, 4000u ) ;
while( !hasTimerExpired( &display_time ) )
{
adc1_prcs();
adc2_prcs();
}
}
Note that I have removed your unnecessary initial display by making it the first display in the loop. It may have been there for a reason, but I cannot see it.
This can be simplified given:
void displayPage( int page )
{
Lcd_Cmd(_LCD_CLEAR);
switch( page )
{
case 0 :
{
Lcd_Out(1,1,"VAC-IN : ");
Lcd_Out(2,1,"VAC-OUT: ");
}
break ;
case 1 :
{
Lcd_Out(1,1,"info page");
Lcd_Out(2,4,"123456789");
}
break ;
}
}
then
int page = 0 ;
displayPage( page ) ;
timer_t display_time ;
startTimer( &display_time, 4000u ) ;
for(;;)
{
// Read ADCs continuously
adc1_prcs();
adc2_prcs();
// Every four seconds flip display
if( hasTimerExpired( &display_timer ) )
{
startTimer( &display_time, 4000u )
page = page == 0 ? 1 : 0 ;
displayPage( page ) ;
}
}
Note that the timer interface allows multiple concurrent timers of differing expiry periods so that you can have code like:
#define TASK1_PERIOD 1000u ; // 1 second
#define TASK2_PERIOD 333u ; // 1/3 second
timet_t t1, t2 ;
startTimer( &t1, TASK1_PERIOD ) ;
startTimer( &t2, TASK2_PERIOD ) ; // 1/3 second
for(;;)
{
if( hasTimerExpired( &t1 ) )
{
startTimer( &t1, TASK1_PERIOD ) ; // restart
// Things to do every second here...
}
if( hasTimerExpired( &t2 ) )
{
startTimer( &t2, TASK2_PERIOD ) ; // restart
// Things to do every 1/3 second here...
}
// Things to do continuously here...
}
The architecture lends itself to implementing time-triggered state-machines, and periodic single-shot functions.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.