AC frequency meter using the PIC18F2550
This project shows an AC frequency meter using tow interrupts External interrupt and TMR0 interrupt.
Note: I can teach you or help you with yours Microcontroller or FPGA projects. If you interested contact me at:
postgraduatecahg@gmail.com
int externals_tick = 0; // for counting the external interrupts
float frequency = 0;
float T = 0;
// LCD pins confiuration
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RC0_bit;
sbit LCD_D5 at RC1_bit;
sbit LCD_D6 at RC2_bit;
sbit LCD_D7 at RC6_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISC0_bit;
sbit LCD_D5_Direction at TRISC1_bit;
sbit LCD_D6_Direction at TRISC2_bit;
sbit LCD_D7_Direction at TRISC6_bit;
// End LCD module connections
char txt[15];
char txt3[] = "frequency meter";
char txt4[] = "starting";
void interrupt() {
if(INTCON.TMR0IF)
{
//PORTC.F0 = ~PORTC.F0;
T = 32.768/externals_tick; // Tms
frequency = (1/T)*1000;
frequency = frequency/2;
FloatToStr(frequency, txt);
INTCON.TMR0IF=0; // clear the TIMER0 interrupt
TMR0L = 3; // this value is to perform ~32ms
externals_tick = 0;
T0CON.TMR0ON = 1; // TMR0 is running now
}
else
{
externals_tick++;
INTCON.INT0IF=0; // clear the external interrupt
}
}
void main() {
PORTC = 0;
LATC = 0;
TRISC = 0x00;
ADCON1 = 0x0F; // For digital configuration
PORTB = 0;
TRISB = 0xCF;
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_Out(1,3,txt3); // Write text in first row
Delay_ms(1000);
Lcd_Cmd(_LCD_CLEAR); // Clear display
Delay_ms(100);
INTCON.GIE = 1; // global interrups are enable
INTCON.PEIE = 1; // pheriferical interrups.
INTCON.INT0IE = 1; // external interrup enable
INTCON.TMR0IE = 1; // TMR0 interrup enable
INTCON2.INTEDG0 = 1; //1 = Interrupt on rising edge
INTCON2.RBPU = 0; //0= pull up
INTCON2.TMR0IP = 1; //TMR0 Overflow Interrupt Priority bit
T0CON.T08BIT = 1; //8 bits
T0CON.T0CS = 0; //Timer0 Clock Source Select bit,
//0 = Internal instruction cycle clock (CLKO)
T0CON.PSA = 0; //0 = Timer0 prescaler is assigned.
// Timer0 clock input comes from prescaler output.
T0CON.T0PS0 = 1; // Timer0 Prescaler Select bits
T0CON.T0PS1 = 1; //
T0CON.T0PS2 = 1; //
TMR0L = 3;
T0CON.TMR0ON = 1; //start TMR0
while(1){
Lcd_Out(1,1,txt);
delay_ms(100);
}
}
No hay comentarios.:
Publicar un comentario