Sie sind auf Seite 1von 3

#include <p24fj128ga010.

h>
#include <stdlib.h>
#include <math.h>
// PIC24FJ128GA010 Configuration Bit Settings
// 'C' source line config statements
#include <xc.h>
// 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)
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 = 0b00001100; // display ON, cursor off, blink off
TMR1 = 0; while( TMR1<1); // 1 x 64us = 64us
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
}
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 = 0xffef; // C?u hnh AN0 l analog
AD1CON1 = 0x80e4; // C?u hnh conversion trigger t? ??ng
// C?u hnh ng ra s? nguyn khng d?u
// C?u hnh 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 hnh th?i gian l?y m?u l Tad,
// C?u hnh clock A/D b?ng clock h? th?ng
AD1CHS = 4; // C?u hnh ng vo S/H
// Ng vo S/H+ l AN0
// 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){
//int Vout,T;
int a;
//int a;
a = ADC1BUF0;

//Vout = (a*5)/1024;
a=(a*5)/1024;
a=100*a-50;
a=(0.448*a)-50;
//PORTA = T;
//_AD1IF = 0;
lcd_init();
char kq[30];
//
//

//putsLCD("Nhiet do: ");


sprintf(&kq[0],"nhietdo %d",a);
putsLCD(kq);
putLCD(0xdf);
putsLCD(" C");
AD1CON1bits.ADON = 0;
// ??c k?t qu? t? b? ??m ADC1BUF0
IFS0bits.AD1IF = 0; // Xa c? ng?t
}
int main( void)
{
ADC1_Init();
// initializations
while(1)
AD1CON1bits.ADON = 1;;
return 0;
// put a title on the first line
} // main

Das könnte Ihnen auch gefallen