|
楼主 |
发表于 2010-3-27 21:08:56
|
显示全部楼层
here is the code for the Nokia version. the code itself is almost identical to that using the hd44780 lcd.
=========code============
#include <htc.h>
#include <string.h>
#include "GLCDNokia7110.h"
__CONFIG(BORDIS & MCLRDIS & PWRTEN & WDTDIS & INTIO);
#define CLK_IN (1<<5) //clk_in on ra5/t1cki. do NOT change
//frequency definitions
#define Freq_LF 1000 //Hz, if less than 1KHz, it is LF
#define Freq_HF 100000 //Hz, if more than 100Khz, it is HF
//high frequency
#define T1_PS_HF 0b11 //tmr1 prescaler for hf
#define Delay_MS_HF 100 //delay for 100ms
//medium frequency
#define T1_PS_MF 0b10 //tmr1 prescaler for mf
#define Delay_MS_MF 500 //delay for 250ms
//low frequency
#define T1_PS_LF 0b00 //tmr1 prescaler for lf
#define Delay_MS_LF 1000 //delay for 1000ms. 1000ms is the max
const unsigned char str0[]="f= Hz";
const unsigned char str1[]="cycle_ct= ";
const unsigned char str2[]="tmr_cnt = ";
const unsigned char str3[]="sTMR0 = ";
unsigned char vRAM[17];
unsigned long tmr_count;
unsigned int cycle_count=0;
unsigned int sTMR0; //shadow tmr0
/**********************************************************
delay routines
***********************************************************/
void delay(unsigned int dly) { //delay dly cycles
for (; dly>0; dly--)
continue;
}
void delay_us(unsigned int us) { //delay us microseconds
for (; us>0; us--)
NOP();
}
void delay_ms(unsigned int ms) { //delay ms milliseconds
for (; ms>0; ms--)
{delay_us(89); NOP(); NOP();}
}
/****************************************
* initialize the mcu
****************************************/
void mcu_init(void) {
// need to initialize the mcu here.
ANSEL=0x00; //turn portA digital
CMCON0=0x07; //turn off comparators
// ANSELH=0x00; //turn portB digital
// TRISC = 0;
IRCF2=1, IRCF1=1, IRCF0=0; //4Mhz
}
void interrupt isr(void) { //interrupt service routine
tmr_count++; //increment the counter
TMR1IF=0; //reset tmr1 flag
}
void tmr1_init(unsigned char T1_PS) { //set up tmr0
tmr_count=0; //reset tmr_count
TMR1IF=0; //reset tmr1 interrupt flag
TMR1ON=0; //turn off tmr1
TMR1H=0; //reset the timer1 high byte
TMR1L=0; //reset the timer1 low byte
T1GE=0; //tmr1 is not gated and always on
T1CKPS1 = T1_PS >> 1; //set up tmr1 prescaler
T1CKPS0 = T1_PS & 0b01;
T1OSCEN = 0; //tmr1 low frequency osc off
T1SYNC = 0; //no tmr1 synchronization
TMR1CS = 1; //count pulses on t1cki
TMR1IE=1; //enable tmr1 interrupt
PEIE=1; //enable peripheral interrupt
GIE=1; //enable interrupt
/* old code on tmr0
T0CS=1; //tmr0 source = t0cki/gpio2
PSA=0; //prescaler assigned to tmr0
PS2=(T0_PS & 0b100) >> 2; //set up tmr0 prescaler
PS1=(T0_PS & 0b010) >> 1;
PS0=(T0_PS & 0b001) >> 0;
T0IE=1; //enable tmr0
GIE=1; //enable the interrupt
*/
}
void ultoa(char *s, unsigned long ul, unsigned char length) { //convert unsigned long to a string
unsigned char i;
i=length;
do {
s[i--]=ul % 10 + '0';
ul = ul / 10;
} while (ul);
}
unsigned long freq_measure(unsigned int dly, unsigned char T1_PS) { //measure the frequency
unsigned long tmp; //temperary variable
tmr1_init(T1_PS); //initialize the tmr0
TMR1ON=1; //turn on tmr1
delay_ms(dly); //delay 100ms. need to adjust the argument for precise timing
TMR1ON=0; //turn off tmr1
sTMR0=TMR1L + ((unsigned int) TMR1H <<8 ); //save the tmr0 counter
//tmp=((((unsigned long) tmr_count<<16) | sTMR0)<<(T1_PS))*(1000/dly); //update tmr_count
//tmp = (unsigned long) tmr_count<<16;
tmp = ((unsigned long) tmr_count<<16) | sTMR0;
tmp = tmp << (T1_PS);
tmp = tmp * 1000/dly;
return tmp; //update tmr_count
}
int main(void) {
unsigned long freq;
mcu_init(); //initialize the mcu
lcd_init(); //initilize the lcd
lcd_clear();
TRISA |= CLK_IN; //set clk_in as input;
strcpy(vRAM, "16F684 Frequency"); lcd_display(LCD_Line0, vRAM, LCD_NORMAL);
strcpy(vRAM, " Hello, "); lcd_display(LCD_Line6, vRAM, LCD_NORMAL);
strcpy(vRAM, "Left + Right"); lcd_display(LCD_Line7, vRAM, LCD_NORMAL);
while (1) {
cycle_count+=1;
strcpy(vRAM, str1); ultoa(&vRAM[8], cycle_count, 7); lcd_display(LCD_Line3, vRAM, LCD_NORMAL);
freq=freq_measure(Delay_MS_HF, T1_PS_HF); //default is to measure HF
if (freq<Freq_HF) freq=freq_measure(Delay_MS_MF, T1_PS_MF); //then mf
if (freq<Freq_LF) freq=freq_measure(Delay_MS_LF, T1_PS_LF); //then lf
strcpy(vRAM, str2); ultoa(&vRAM[8], tmr_count, 7); lcd_display(LCD_Line4, vRAM, LCD_NORMAL);
strcpy(vRAM, str3); ultoa(&vRAM[8], sTMR0, 7); lcd_display(LCD_Line5, vRAM, LCD_NORMAL);
//sprintf(vRAM, "f=%12uHz", freq); lcd_display(LCD_Line1, vRAM, LCD_NORMAL);
strcpy(vRAM, str0); ultoa(&vRAM[1], freq, 12); lcd_display(LCD_Line1, vRAM, LCD_NORMAL);
}
} |
|