/*
* Author : Asim Siddiqui
* Version : 1.01
* Date : 07/04/2014
* Note : Tested on H/W
* PIC : Prog for PIC18F46k22 also compatible with PIC18F45K22
* H/W Conn : LCD PORTD, FREQ on PORTB.0, Switch PORTB.1, PWM CCP1 and CCP2
* Contact : itsasim@live.com
* Note : Use Timer0 as timer and Timer1 as counter
*/
sbit LCD_RS at LATC5_bit;
sbit LCD_EN at LATC4_bit;
sbit LCD_D4 at LATD4_bit;
sbit LCD_D5 at LATD5_bit;
sbit LCD_D6 at LATD6_bit;
sbit LCD_D7 at LATD7_bit;
// Pin direction
sbit LCD_RS_Direction at TRISC5_bit;
sbit LCD_EN_Direction at TRISC4_bit;
sbit LCD_D7_Direction at TRISD4_bit;
sbit LCD_D6_Direction at TRISD5_bit;
sbit LCD_D5_Direction at TRISD6_bit;
sbit LCD_D4_Direction at TRISD7_bit;
// Lcd module connections
/*sbit LCD_RS at LATB4_bit;
sbit LCD_EN at LATB5_bit;
sbit LCD_D4 at LATB0_bit;
sbit LCD_D5 at LATB1_bit;
sbit LCD_D6 at LATB2_bit;
sbit LCD_D7 at LATB3_bit;
// Pin direction
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D7_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB0_bit;*/
//long int a[20]={0}; //array for storing the measured frequency
//long int z=0;
int c2=0; //var. used for storing the frequency measured in the array
int len=20;
void main()
{
unsigned long frequency,y; //frequency1,frequency2,temp=0; // complete frequency count
int i,t=0;
unsigned long freq_low; // low word of frequency count
char freq_txt[11]; // buffer for ASCII conversion
char y1[11]; // buffer for ASCII conversion
ANSELA = 0; // Configure PORTA pins as digital
ANSELB = 0; // Configure PORTB pins as digital
ANSELC = 0; // Configure PORTC pins as digital
ANSELD = 0; // Configure PORTD pins as digital
ADCON0 = 0; // A/D off
ADCON1 = 0x0f; // all digital
CM1CON0 = 0; // disable 18F45K22 comparator
CM2CON0 = 0; // disable 18F45K22 comparator
TRISB = 0; // PORTB is output
PORTB = 0x00; // Initialize PORTB
LATA = 0; // all off
LATB = 0xff; // disable GLCD if fitted
LATC = 0; // all off
TRISA = 0xC0; // all output except OSC pins
TRISB = 0x00; // all output
TRISC = 0x01; // all output except RC0
Delay_ms(100); // Wait for LCD to stabilise
Lcd_Init(); // Initialize Lcd
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Turn cursor off
Lcd_Out(1,1, " Active RFID Tag "); // Print text to Lcd, 1st row, 1st column
Lcd_Out(2,1, "Freq: 0000000000 Hz "); // Print text to Lcd, 2nd row, 1st column
Lcd_Out(3,1, "PPM: 0000000000 "); // Print text to Lcd, 3st row, 1st column
Lcd_Out(4,1, " JMI and IITD ");
UART1_Init(9600); // Initialize UART module
Delay_ms(20); // mandatory delay
UART1_Write_Text("Start"); // tx activity
UART1_Write(13);
UART1_Write(10);
T0CON = 0x86; // T0 on, 16 bit, prescaler 128
T1CON = 0x85; // T1 on, ext input, no prescale
T1GCON = 0x00; // no gating
while(1)
{
TMR0H = 0x0B; // Timer set for 999856us
// Timer set for 999984us for rekha board
TMR0L = 0xDD;
INTCON.TMR0IF = 0; // reset time-out indicator
frequency = 0; // initialise count
TMR1L = 0; // reset to 0
TMR1H = 0; // reset to 0
PIR1.TMR1IF = 0; // initialise
while(!INTCON.TMR0IF)
{ // wait for 999856us time-out
if(PIR1.TMR1IF)
{ // monitor frequency count overflow
PIR1.TMR1IF=0; // reset overflow
frequency += 65536; // increment count high bytes
}
}
freq_low = (TMR1H << 8) + TMR1L; // collect frequency bytes
frequency += freq_low; // collect frequency bytes
y = -0.0481*frequency + 6435.3;
LongWordToStrWithZeros(frequency, freq_txt); // covert result to ASCII
LongWordToStrWithZeros(y, y1); // covert result to ASCII
//temp=0;
Lcd_Out(2,7, freq_txt); // Print text to Lcd, 2nd row, 1st column
Lcd_Out(3,7, y1); // Print text to Lcd, 2nd row, 1st column
//LATA=LATA^1; // show some life by toggling LED
UART1_write_text(freq_txt);
}
}