It is currently 20 Apr 2018 01:51

All times are UTC + 1 hour




Post new topic Reply to topic  [ 1 post ] 
Author Message
PostPosted: 31 Dec 2017 00:29 
Offline

Joined: 30 Dec 2017 21:48
Posts: 2
Hi! I'm new on MikroC and i need a algorithm that make a analog read a start of program, to verify the battery tension, showing on a LCD display 16x2, blinking 3 times and then goes off to enter the loop for frequency counter. I got a frequency counter source code from best-microcontroller-projects.com and just add a simple analog read command before the infinite loop. When i tested it on Proteus ISIS the program shows the voltage on LCD correctly but, when the code for analog read finishes, the LCD shows nothing, the frequency counter code seems to be no nonexistent, not read by the microcontroller or something like that. If i remove the analog read code part from the algorithm, the frequency is showed perfect on LCD.

Someone can help me??? I don't understand the reason for this incompatibility between this codes. I Need this program for my job and i will be very grateful to anyone who helps me.

Note: I am using a PIC16F877A

Thanks!

#include "bit.h"
#include "ltoa.h"

// LCD Connections Lcd_Config(&PORTB,0,2,1,7,6,5,4); // PORT,RS,EN,WR,D7,D6,D5,D4
sbit LCD_RS at RB0_bit ;
sbit LCD_EN at RB2_bit ;
sbit LCD_D4 at RB4_bit ;
sbit LCD_D5 at RB5_bit ;
sbit LCD_D6 at RB6_bit ;
sbit LCD_D7 at RB7_bit ;

sbit LCD_RS_Direction at TRISB0_bit ;
sbit LCD_EN_Direction at TRISB2_bit ;
sbit LCD_D4_Direction at TRISB4_bit ;
sbit LCD_D5_Direction at TRISB5_bit ;
sbit LCD_D6_Direction at TRISB6_bit ;
sbit LCD_D7_Direction at TRISB7_bit ;

//
// Globals
//
unsigned long TMR1_counter=0; // Count overflow from interrupt.
unsigned long TMR0_counter=0; // Count overflow from interrupt.
//
// Flags
//
short update_LCD = 0;
short toggle_LED = 0;
short do_TMR0_end_count = 0; // Store Timer 1 low byte.
//
// Frequency Counter values.
//
unsigned short st_TMR1L ; // Store Timer 1 low byte.
unsigned short st_TMR1H ; // store Timer 1 high byte.
unsigned long st_TMR1_ovfl=0 ; // store timer 1 overflows - multiples of 65536.

////////////////////////////////////////////////////////////////////////
void init_ports(void) {

TRISA=0xFF;
ADCON0=0x01;
ADCON1=0x0E;

PORTA = 1;
// inputs except RA1,RA0 p to 4017 (7seg when used)

PORTB = 0; // Note RB1 (R/Wn) New LCD routine does not use: keep low (W)
TRISB = 0; // out

PORTC = 0x01; // b0 = 1 for 7seg
TRISC = 0x81; // B7 = i/p for USART, RC0 T1CKI-Timer1 i/p.

PORTD = 0;
TRISD = 0; // out

PORTE = 0;
TRISE = 0; // out
}

////////////////////////////////////////////////////////////////////////
void init_interrupts() {

// T1: prescale = 0, osc. off, timer on, external clock, unsynchronized.
T1CON = (1<<TMR1ON) | (1<<TMR1CS) | (1<<T1SYNC);
PIR1.TMR1IF = 0;
PIE1.TMR1IE = 1;

// T0: T0SE=0(N/A) ,TOCS=0(FOSC/4),PSA=1(no pres.),PS2..0=0 (N/A).
OPTION_REG = (1<<PSA);
}

////////////////////////////////////////////////////////////////////////
void start_timer_count() {

INTCON.GIE = 0; // Disable All interrupts.
INTCON.PEIE = 0; // Disable All Extended interrupts.

TMR1H = 0; // Clear Timer1 high count. stop overflow.
PIR1 &= ~(1<<TMR1IF); // Clear Interrupt Flag.
TMR1L = 0; // Clear Timer1 low count.
TMR1_counter = 0; // Clear overflow counter.

TMR0 = 0; // Reset Timer0 value.
INTCON.TMR0IF = 0; // Clear Timer0 int flag
INTCON.TMR0IE = 1; // Enable Timer0 interrupt.

INTCON.PEIE = 1; // Enable All Extended interrupts.
INTCON.GIE = 1; // Enable All interrupts.
}

////////////////////////////////////////////////////////////////////////
void interrupt() {

if (PIR1.TMR1IF) {
TMR1_counter++;
PIR1.TMR1IF = 0;
}

if (INTCON.TMR0IF) {
TMR0_counter++;

if(do_TMR0_end_count) { // The last count is not a full overflow.

st_TMR1L = TMR1L; // Capture counter values at this point.
st_TMR1H = TMR1H;
st_TMR1_ovfl = TMR1_counter;
TMR1_counter=0;
TMR0_counter=0;
update_LCD=1; // Flag for update.

do_TMR0_end_count=0;

} else {

if(TMR0_counter==3906) { // Capture the input count.
do_TMR0_end_count=1;
TMR0 = 256-64+2; // 2 cycles lost when writing to TMR0 so add 2.
toggle_LED=1;
}

if(TMR0_counter==1953) { toggle_LED=1;}

// if((TMR0_counter%976)==0) { toggle_LED=1;} // ~.25s // too long.
}

INTCON.TMR0IF = 0;

} // End TMR0IF
}

////////////////////////////////////////////////////////////////////////
// Start here
//
void main() {
char digit[]="0.0 V";
char op[12]; // Display string for ascii converted long.
char opdb[12]; // double buffer to stop flicker.
unsigned long freq = 0, temp; // display frequency value to use.
unsigned short blinkc=0; // blink counter
int i,n;


Lcd_Init ();
Lcd_Cmd ( _LCD_CLEAR );
Lcd_Cmd ( _LCD_CURSOR_OFF );





init_ports();
init_interrupts();
start_timer_count();

temp = ADC_Read(0);
temp=temp*500/102;
digit[0]=(temp/1000)+48;
digit[2]=((temp%1000)/100)+48;
LCD_Out(1,3,"BATTERY TEST");
LCD_Out(2,6,digit);
Delay_ms(500);
LCD_Cmd(_LCD_CLEAR);
Delay_ms(200);
LCD_Out(1,3,"BATTERY TEST");
LCD_Out(2,6,digit);
Delay_ms(500);
LCD_Cmd(_LCD_CLEAR);
Delay_ms(200);
LCD_Out(1,3,"BATTERY TEST");
LCD_Out(2,6,digit);
Delay_ms(500);
LCD_Cmd(_LCD_CLEAR);
Delay_ms(200);


for (;;) {


if (update_LCD) {


INTCON.GIE = 0; // Disable All interrupts.
INTCON.PEIE = 0; // Disable All Extended interrupts.

freq = (st_TMR1L+(st_TMR1H<<8)+(st_TMR1_ovfl<<16))*0.335;

ltoa(freq, op, 10);

n=ltoa(freq, opdb, 10); // Build string in non display buffer
memcpy(op,opdb,n); // Copy digits
memset(&op[n],' ',12-n); // Blank the rest.

LCD_Out(1,1,"FREQ:");
LCD_Out(1,7,op);

update_LCD=0;

freq = (st_TMR1L+(st_TMR1H<<8)+(st_TMR1_ovfl<<16))*0.8375;
ltoa(freq, op, 10);

n=ltoa(freq, opdb, 10); // Build string in non display buffer
memcpy(op,opdb,n); // Copy digits
memset(&op[n],' ',12-n); // Blank the rest.

LCD_Out(2,1,"NR:");
LCD_Out(2,5,op);

update_LCD=0;

TMR1_counter=0;
TMR0_counter=0;

start_timer_count();
}

if (toggle_LED) { // Also check for signal presence at TMR1.
blinkc=~blinkc;
if (blinkc==0) { setBit(PORTD,0); } else { resBit(PORTD,0); }
toggle_LED=0;

if (freq==0) {
for ( i=0;i<12;i++) { op[i]=' ';}
LCD_Out(1,7,op);
}
}
}
}


Attachments:
dasdasd.jpg
dasdasd.jpg [ 258.9 KiB | Viewed 167 times ]
Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: