Professional Documents
Culture Documents
Dich CH Trên LCD Pic 24
Dich CH Trên LCD Pic 24
h>
#include <stdlib.h>
#include <math.h>
// PIC24FJ128GA010 Configuration Bit Settings
// 'C' source line config statements
// CONFIG2
#pragma config POSCMOD = NONE
// Primary Oscillator Select (Primary os
cillator disabled)
#pragma config OSCIOFNC = OFF
// Primary Oscillator Output Function (O
SC2/CLKO/RC15 functions as CLKO (FOSC/2))
#pragma config FCKSM = CSDCMD
// Clock Switching and Monitor (Clock sw
itching and Fail-Safe Clock Monitor are disabled)
#pragma config FNOSC = FRCDIV
// Oscillator Select (Fast RC Oscillator
with Postscaler (FRCDIV))
#pragma config IESO = ON
// Internal External Switch Over Mode (I
ESO mode (Two-Speed Start-up) enabled)
// CONFIG1
#pragma config WDTPS = PS32768
// Watchdog Timer Postscaler (1:32,768)
#pragma config FWPSA = PR128
// WDT Prescaler (Prescaler ratio of 1:1
28)
#pragma config WINDIS = ON
// Watchdog Timer Window (Standard Watch
dog Timer enabled,(Windowed-mode is disabled))
#pragma config FWDTEN = ON
// Watchdog Timer Enable (Watchdog Timer
is enabled)
#pragma config ICS = PGx2
// Comm Channel Select (Emulator/debugge
r uses EMUC2/EMUD2)
#pragma config GWRP = OFF
// General Code Segment Write Protect (W
rites to program memory are allowed)
#pragma config GCP = OFF
// General Code Segment Code Protect (Co
de protection is disabled)
#pragma config JTAGEN = OFF
// JTAG Port Enable (JTAG port is disabl
ed)
void lcd_init()
{
// PMP initialization
PMCON = 0x83BF; // Enable the PMP, long waits
PMMODE = 0x3FF; // Master Mode 1
PMAEN = 0x0001; // PMA0 enabled
// init TMR1
T1CON = 0x8030; // Fosc/2, prescaled 1:256, 64us/tick
// wait for >30ms
TMR1 = 0; while( TMR1<500); // 500 x 64us = 32ms
PMADDR = LCDCMD; // select the command register (ADDR = 0)
PMDATA = 0b00111000; // function set: 8-bit interface, 2 lines, 5x7
TMR1 = 0; while( TMR1<1); // 1 x 64us = 64us
PMDATA = 0b00011100; // display ON, cursor off, blink off
delay(3000);
PMDATA = 0b00011100; // display ON, cursor off, blink off
delay(3000);
PMDATA = 0b00011100; // display ON, cursor off, blink off
delay(3000);
PMDATA = 0b00011100; // display ON, cursor off, blink off
delay(3000);
PMDATA = 0b00011100; // display ON, cursor off, blink off
delay(3000);
PMDATA = 0b00011100; // display ON, cursor off, blink off
delay(3000);
PMDATA = 0b00011100; // display ON, cursor off, blink off
delay(3000);
PMDATA = 0b00011100; // display ON, cursor off, blink off
delay(3000);
PMDATA = 0b00011100; // display ON, cursor off, blink off
delay(3000);
PMDATA = 0b00011100; // display ON, cursor off, blink off
delay(3000);
PMDATA = 0b00011100; // display ON, cursor off, blink off
delay(3000);
PMDATA = 0b00000001; // clear display
TMR1 = 0; while( TMR1<25); // 25 x 64us = 1.6ms
PMDATA = 0b00000110; // increment cursor, no shift
TMR1 = 0; while( TMR1<25); // 25 x 16us = 1.6ms
}
void delay(unsigned int ms)
{
unsigned int i;
unsigned char j;
for(i=0;i<ms;i++)
{
for(j=0;j<125;j++){}
}
}
char LCDread( int addr)
{
int dummy;
while(PMMODEbits.BUSY); // wait for PMP to complete previous commands
PMADDR = addr; // select the command address
Vout,T;
long a;
a = ADC1BUF0;
Vout = (a*5)/1024;
T=100*Vout-50;
//
PORTA = T;
lcd_init();
//putsLCD("nhiet do : ");
//putLCD(nguyen);
putLCD(phan10);
putLCD(phan100);
PORTA = T;
putLCD(0xdf);
putsLCD("C");
//putsLCD(" V");
AD1CON1bits.ADON = 0;
// ??c k?t qu? t? b? ??m ADC1BUF0
IFS0bits.AD1IF = 0; // Xa c? ng?t
}
int main( void)
{
TRISA = 0;
//
PORTA=0x00;
//
delay(200);
//
PORTA=0xff;
//
delay(200);
ADC1_Init();
// initializations
while(1)
AD1CON1bits.ADON = 1;;
return 0;
// put a title on the first line
} // main