israel_Y
Member level 1
- Joined
- Feb 8, 2010
- Messages
- 34
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,286
- Location
- The Netherlands
- Activity points
- 1,534
I m writing frequency counter for 16f877A using mikroc: my code is seen below. please guide me where it is wrong as it doesnt give any output when i run it in proteus.
[moved from analog design section]
Code:
// Define LCD module connections.
sbit LCD_RS at RD1_bit;
sbit LCD_EN at RD0_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 LCD_RS_Direction at TRISD1_bit;
sbit LCD_EN_Direction at TRISD0_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;
// End LCD module connection definition
//global variables
char *freq = "0000000.00";
long count, accum;
float frequency;
short ccp1int, tmr1int, i;
void Display_Freq(float freq2write) {
int a=0;
a =FloatToStr(freq2write, freq);
if (a != 0)
Lcd_Out(1, 1, "unsuccessful ");
else {
Lcd_Cmd(_LCD_CLEAR); // CLEAR display
Lcd_Out(1, 1, freq);
Lcd_Out(2,10, "in Hz");
}
}
// Interrupt service routine
void interrupt() {
if(PIR1.CCP1IF) {
ccp1int = 1;
accum = count - CCPR1;
count = CCPR1;
}
if(PIR1.TMR1IF){
tmr1int ++;
PIR1.TMR1IF = 0; //reset timer1 flag
}
}
void main() {
ADCON1 = 0x06; //All I/O pins are configured as digital
TRISC = 0xFF;
// TMR1 settings
T1CON.TMR1CS = 0; // timer mode Fosc/4
PIE1.TMR1IE = 1; //enable TMR1 inturrupt
T1CON.T1CKPS0 = 1; //prescaler 1:4
T1CON.T1CKPS1 = 1;
//CCP1 setting
PIE1.CCP1IE = 0; //disable ccp1 inturrupt
CCP1CON = 0x05; //CCP1 every fourth rising edge
PIR1.CCP1IF = 0; //avoid posible fals inturrupt
PIE1.CCP1IE = 1; //enable ccp1 inturrupt
INTCON.PEIE = 1; //enable all low priority peripheral interrupts
INTCON.GIE = 1; //Enable Global Inturrupt
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR); // CLEAR display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_Out(1,1,"Freq conversion");
delay_ms(5000);
for(;;)
{
T1CON.TMR1ON = 1; //start timer1
for(i = 0; i < 2; i++) //to catch two consecutive ccp1 interrupts
{
do{}while(!ccp1int);
ccp1int = 0; //reset ccp1 flag indicator
}
T1CON.TMR1ON = 0; //stop timer1
accum = tmr1int*65356 + accum;
frequency = (Get_Fosc_kHz())/accum;
Display_Freq(frequency);
count = 0; //reset count
tmr1int = 0; //reset timer1 flag indicator
accum = 0; //reset accumulator
}
}
[moved from analog design section]