ana_cont
Junior Member level 2
Code:
//LCD module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
//end LCD module connections
#define PULSE PORTA.RA4
unsigned int Overflow;
unsigned char Cnt;
//
//Timer interupt service routine jumps here at every 10ms
//
void interrupt(void)
{
if(INTCON.TMR0IF==1) //If TIMER0 INTERRUPT
{
Overflow++; //Increment Overflow flag
INTCON.TMR0IF=0; //Clear timer0 interrupt flag
}
if(PIR1.TMR1IF==1) //If timer1 interrupt
{
TMR1H=0x0B; //Reload timer register
TMR1L=0xDC;
Cnt++; //Increment cnt
PIR1.TMR1IF=0; //Clear Timer1 interrupt flag
}
}
void main()
{
unsigned char Txt[11];
unsigned long Elapsed;
unsigned char L_Byte, H_Byte;
ADCON1 = 0x0F; // Configure all ports with analog function as digital
CMCON = 0x07; // Disable comparators
//ANSEL A =0;// 0; // Configure PORTA as digital
//ANSEL B = 0; //
TRISA.RA4 = 1; // RA4 is input
Lcd_Init(); // Inialize LCD
Lcd_Cmd(_LCD_CURSOR_OFF); // Disable cursor
//
// Configure TIMER0 for 16-bit mode, no prescaler, clock provided by external
// signal into pin T0CKI. Timer is not started here
//
T0CON = 0x28; // TIMER0 16-bit,no prescaler,T0CKI clk
//
// Configure Timer1 for 250 ms Overflow. Timer1 is not started here
//
T1CON = 0x36;
PIE1 = 0x01; // Enable TIMER1 interrupts
PIR1.TMR1IF = 0; // Clear TIMER1 interrupt flag
INTCON = 0xE0; // Enable TIMER0 and TIMER1 interrupts
for(;;) // Wait for interrupts
{
TMR0H = 0; // Clear Timer0 registers
TMR0L = 0;
TMR1H = 0x0B; // Load Timer1 registers
TMR1L = 0xDC;
//
Overflow = 0; // Clear Overflow count
Cnt = 0; // Clear Cnt
//
// Start TIMER0. It will increment each me an external pulse is detected.
// TIMER0 increments on the rising edge of the external clock
//
T0CON.TMR0ON = 1;
//
// Start Timer1 to count 4 × 250 ms = 1 s
//
T1CON.TMR1ON = 1;
while(Cnt != 4); // Wait unl 1 s has elapsed
//
// 1 s has elapsed. Stop both mers
//
T0CON.TMR0ON = 0; // Stop TIMER0
T1CON.TMR1ON = 0; // Stop Timer1
// Get TIMER0 count
L_Byte = TMR0L;
H_Byte = TMR0H;
//
// Store TIMER0 count is variable Elapsed
//
Elapsed = (unsigned long)256 * H_Byte + L_Byte;
Elapsed = 65536 * Overflow + Elapsed;
//
// Convert into string and display
//
LongWordToStr(Elapsed, Txt); // Long to string conversion
Lcd_Cmd(_LCD_CLEAR); // Clear LCD
Lcd_Out(1,1,"Frequency (HZ)"); // Display heading
Lcd_Out(2,1,Txt); // Display measured frequency
Delay_ms(1000); // Wait 1 s and repeat
}
}
--- Updated ---
hI CMCON=7 ; NOT COMPILE
Last edited by a moderator: