You are on page 1of 4

#include <p24fj128ga010.

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)

#define LCDDATA 1 // RS = 1 ; access data register


#define LCDCMD 0 // RS = 0 ; access command register
#define PMDATA PMDIN1 // PMP data buffer
#define LCDbusy() LCDread(LCDCMD) & 0x80
#define LCDaddr() LCDread(LCDCMD) & 0x7F
#define getLCD() LCDread(LCDDATA)
#define putLCD(d) LCDwrite(LCDDATA,(d))
#define LCDcmd(c) LCDwrite(LCDCMD,(c))
#define LCDhome() LCDwrite(LCDCMD,2)
#define LCDclr() LCDwrite(LCDCMD,1)
int v,T;

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

dummy = PMDATA; // initiate a read cycle, dummy read


while(PMMODEbits.BUSY); // wait for PMP to complete the sequence
return( PMDATA); // read the status register
} // LCDread
void LCDwrite( int addr, char c)
{
while( LCDbusy());
while( PMMODEbits.BUSY); // wait for PMP to be available
PMADDR = addr;
PMDATA = c;
} // LCDwrite
void putsLCD( char *s)
{
while(*s)
putLCD( *s++);
} //putsLCD
// adc *******************************************************
void ADC1_Init(void){
AD1PCFG = 0xffdf; // C?u h?nh AN0 l analog
AD1CON1 = 0x80e4; // C?u h?nh conversion trigger t? ??ng
// C?u h?nh ng? ra s? nguyn khng d?u
// C?u h?nh chuy?n ??i t? ??ng
AD1CON2 = 0; // ?i?n th? tham chi?u l AVdd v AVss
// Khng scan ng? vo
// Ng?t x?y ra sau m?i l?n chuy?n ??i
// Lun ch?n MUXA ??a vo b? S/H
AD1CON3 = 0x0100; // C?u h?nh th?i gian l?y m?u l Tad,
// C?u h?nh clock A/D b?ng clock h? th?ng
AD1CHS = 4; // C?u h?nh ng? vo S/H
// Ng? vo S/H+ l AN0
AD1PCFGbits.PCFG4 = 0;
// ng? vo S/H- l Vr- (AVss).
AD1CSSL = 0; // Khng scan ng? vo
IFS0bits.AD1IF = 0; // Xa c? ng?t ADC
// ?? ?u tin ng?t m?c ??nh l 4
IEC0bits.AD1IE = 1; // Cho php ng?t ADC
AD1CON1bits.ADON = 1; // B?t ADC
}
// ADC ISR
void __attribute__ ((__interrupt__, auto_psv)) _ADC1Interrupt(void){
long x;
int nguyen;
int phan10;
int phan100;
x=ADC1BUF0;
v=(x*5)/1024;
T=100*v-50;
phan100=(T%10)+48;
phan10=((T/10)%10)+48;
nguyen=(T/100)+48;
//int
//
//
//
//
//

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

You might also like