Anh nào giỏi pic16f8 giúp em với >>> khi em dùng 16f877a mô phỏng thì nó hiển thị được nhiệt độ trên lcd, còn dùng con 16f887 thì hiện 0 độ >>> máy ngày rồi mà chưa biết lỗi gì, em lỡ mua con 16f887 rồi, thanks
************************** CODE MAIN **************
#include <16F887.h>
#device *=16 adc=10
#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT,NOLVP, NOCPD, NOWRT
#use delay(clock=20000000)
#include <LCD4bit.h> // Thu vien ham cho LCD
#include "1wire.c"
#include "DS18B20.c"
float temperature;
//-----------------------------------------------------------------
void main()
{
LCD_Init();
//==== Khoi tao cho ngat ngoai============================================= ==
//enable_interrupts (INT_EXT);
//ext_int_edge(H_TO_L);
//enable_interrupts (GLOBAL);
//=========== Khoi tao che do cho bo ADC=====================================
LCD_Char("IT'S VERY HOT");
while(true)
{
temperature=ds18b20_read();
LCD_Cmd(0xC0);
Printf(LCD_Char,"%2.0f",temperature);
LCD_Char(223);
LCD_Char("C");
}
}
*************THU VIEN ***********************
//////// Standard Header file for the PIC16F887 device ////////////////
#device PIC16F887
#nolist
#define PIN_A0 40
#define PIN_A1 41
#define PIN_A2 42
#define PIN_A3 43
#define PIN_A4 44
#define PIN_A5 45
#define PIN_A6 46
#define PIN_A7 47
#define PIN_B0 48
#define PIN_B1 49
#define PIN_B2 50
#define PIN_B3 51
#define PIN_B4 52
#define PIN_B5 53
#define PIN_B6 54
#define PIN_B7 55
#define PIN_C0 56
#define PIN_C1 57
#define PIN_C2 58
#define PIN_C3 59
#define PIN_C4 60
#define PIN_C5 61
#define PIN_C6 62
#define PIN_C7 63
#define PIN_D0 64
#define PIN_D1 65
#define PIN_D2 66
#define PIN_D3 67
#define PIN_D4 68
#define PIN_D5 69
#define PIN_D6 70
#define PIN_D7 71
#define PIN_E0 72
#define PIN_E1 73
#define PIN_E2 74
#define PIN_E3 75
////////////////////////////////////////////////////////////////// Useful defines
#define FALSE 0
#define TRUE 1
#define BYTE int8
#define BOOLEAN int1
#define getc getch
#define fgetc getch
#define getchar getch
#define putc putchar
#define fputc putchar
#define fgets gets
#define fputs puts
////////////////////////////////////////////////////////////////// Control
// Control Functions: RESET_CPU(), SLEEP(), RESTART_CAUSE()
// Constants returned from RESTART_CAUSE() are:
#define WDT_FROM_SLEEP 3
#define WDT_TIMEOUT 11
#define MCLR_FROM_SLEEP 19
#define MCLR_FROM_RUN 27
#define NORMAL_POWER_UP 25
#define BROWNOUT_RESTART 26
////////////////////////////////////////////////////////////////// Timer 0
// Timer 0 (AKA RTCC)Functions: SETUP_COUNTERS() or SETUP_TIMER_0(),
// SET_TIMER0() or SET_RTCC(),
// GET_TIMER0() or GET_RTCC()
// Constants used for SETUP_TIMER_0() are:
#define T0_INTERNAL 0
#define T0_EXT_L_TO_H 32
#define T0_EXT_H_TO_L 48
#define T0_DIV_1 8
#define T0_DIV_2 0
#define T0_DIV_4 1
#define T0_DIV_8 2
#define T0_DIV_16 3
#define T0_DIV_32 4
#define T0_DIV_64 5
#define T0_DIV_128 6
#define T0_DIV_256 7
#define T0_8_BIT 0
#define RTCC_INTERNAL 0 // The following are provided for compatibility
#define RTCC_EXT_L_TO_H 32 // with older compiler versions
#define RTCC_EXT_H_TO_L 48
#define RTCC_DIV_1 8
#define RTCC_DIV_2 0
#define RTCC_DIV_4 1
#define RTCC_DIV_8 2
#define RTCC_DIV_16 3
#define RTCC_DIV_32 4
#define RTCC_DIV_64 5
#define RTCC_DIV_128 6
#define RTCC_DIV_256 7
#define RTCC_8_BIT 0
// Constants used for SETUP_COUNTERS() are the above
// constants for the 1st param and the following for
// the 2nd param:
////////////////////////////////////////////////////////////////// WDT
// Watch Dog Timer Functions: SETUP_WDT() or SETUP_COUNTERS() (see above)
// RESTART_WDT()
// WDT base is 18ms
//
#define WDT_18MS 8
#define WDT_36MS 9
#define WDT_72MS 10
#define WDT_144MS 11
#define WDT_288MS 12
#define WDT_576MS 13
#define WDT_1152MS 14
#define WDT_2304MS 15
// One of the following may be OR'ed in with the above:
#define WDT_ON 0x4100
#define WDT_OFF 0
#define WDT_DIV_16 0x100
#define WDT_DIV_8 0x300
#define WDT_DIV_4 0x500
#define WDT_DIV_2 0x700
#define WDT_TIMES_1 0x900 // Default
#define WDT_TIMES_2 0xB00
#define WDT_TIMES_4 0xD00
#define WDT_TIMES_8 0xF00
#define WDT_TIMES_16 0x1100
#define WDT_TIMES_32 0x1300
#define WDT_TIMES_64 0x1500
#define WDT_TIMES_128 0x1700
////////////////////////////////////////////////////////////////// Timer 1
// Timer 1 Functions: SETUP_TIMER_1, GET_TIMER1, SET_TIMER1
// Constants used for SETUP_TIMER_1() are:
// (or (via |) together constants from each group)
#define T1_DISABLED 0
#define T1_INTERNAL 5
#define T1_EXTERNAL 7
#define T1_EXTERNAL_SYNC 3
#define T1_CLK_OUT 8
#define T1_DIV_BY_1 0
#define T1_DIV_BY_2 0x10
#define T1_DIV_BY_4 0x20
#define T1_DIV_BY_8 0x30
#define T1_GATE 0x40
#define T1_GATE_INVERTED 0xC0
////////////////////////////////////////////////////////////////// Timer 2
// Timer 2 Functions: SETUP_TIMER_2, GET_TIMER2, SET_TIMER2
// Constants used for SETUP_TIMER_2() are:
#define T2_DISABLED 0
#define T2_DIV_BY_1 4
#define T2_DIV_BY_4 5
#define T2_DIV_BY_16 6
////////////////////////////////////////////////////////////////// CCP
// CCP Functions: SETUP_CCPx, SET_PWMx_DUTY
// CCP Variables: CCP_x, CCP_x_LOW, CCP_x_HIGH
// Constants used for SETUP_CCPx() are:
#define CCP_OFF 0
#define CCP_CAPTURE_FE 4
#define CCP_CAPTURE_RE 5
#define CCP_CAPTURE_DIV_4 6
#define CCP_CAPTURE_DIV_16 7
#define CCP_COMPARE_SET_ON_MATCH 8
#define CCP_COMPARE_CLR_ON_MATCH 9
#define CCP_COMPARE_INT 0xA
#define CCP_COMPARE_RESET_TIMER 0xB
#define CCP_PWM 0xC
#define CCP_PWM_PLUS_1 0x1c
#define CCP_PWM_PLUS_2 0x2c
#define CCP_PWM_PLUS_3 0x3c
#word CCP_1 = getenv("SFR:CCPR1L")
#byte CCP_1_LOW = getenv("SFR:CCPR1L")
#byte CCP_1_HIGH = getenv("SFR:CCPR1H")
// The following should be used with the ECCP unit only (or these in)
#define CCP_PWM_H_H 0x0c
#define CCP_PWM_H_L 0x0d
#define CCP_PWM_L_H 0x0e
#define CCP_PWM_L_L 0x0f
#define CCP_PWM_FULL_BRIDGE 0x40
#define CCP_PWM_FULL_BRIDGE_REV 0xC0
#define CCP_PWM_HALF_BRIDGE 0x80
#define CCP_SHUTDOWN_ON_COMP1 0x100000
#define CCP_SHUTDOWN_ON_COMP2 0x200000
#define CCP_SHUTDOWN_ON_COMP 0x300000
#define CCP_SHUTDOWN_ON_INT0 0x400000
#define CCP_SHUTDOWN_ON_COMP1_INT0 0x500000
#define CCP_SHUTDOWN_ON_COMP2_INT0 0x600000
#define CCP_SHUTDOWN_ON_COMP_INT0 0x700000
#define CCP_SHUTDOWN_AC_L 0x000000
#define CCP_SHUTDOWN_AC_H 0x040000
#define CCP_SHUTDOWN_AC_F 0x080000
#define CCP_SHUTDOWN_BD_L 0x000000
#define CCP_SHUTDOWN_BD_H 0x010000
#define CCP_SHUTDOWN_BD_F 0x020000
#define CCP_SHUTDOWN_RESTART 0x80000000
#define CCP_PULSE_STEERING_A 0x01000000
#define CCP_PULSE_STEERING_B 0x02000000
#define CCP_PULSE_STEERING_C 0x04000000
#define CCP_PULSE_STEERING_D 0x08000000
#define CCP_PULSE_STEERING_SYNC 0x10000000
#word CCP_2 = getenv("SFR:CCPR2L")
#byte CCP_2_LOW = getenv("SFR:CCPR2L")
#byte CCP_2_HIGH = getenv("SFR:CCPR2H")
////////////////////////////////////////////////////////////////// SPI
// SPI Functions: SETUP_SPI, SPI_WRITE, SPI_READ, SPI_DATA_IN
// Constants used in SETUP_SPI() are:
#define SPI_MASTER 0x20
#define SPI_SLAVE 0x24
#define SPI_SCK_IDLE_HIGH 0x10
#define SPI_SCK_IDLE_LOW 0x00
#define SPI_CLK_DIV_4 0x00
#define SPI_CLK_DIV_16 0x01
#define SPI_CLK_DIV_64 0x02
#define SPI_CLK_T2 0x03
#define SPI_SS_DISABLED 0x01
#define SPI_XMIT_L_TO_H 0x4000
#define SPI_XMIT_H_TO_L 0x0000
#define SPI_SAMPLE_AT_MIDDLE 0x0000
#define SPI_SAMPLE_AT_END 0x8000
//The following are provided for compatibility
#define SPI_L_TO_H SPI_SCK_IDLE_LOW
#define SPI_H_TO_L SPI_SCK_IDLE_HIGH
////////////////////////////////////////////////////////////////// UART
// Constants used in setup_uart() are:
// FALSE - Turn UART off
// TRUE - Turn UART on
#define UART_ADDRESS 2
#define UART_DATA 4
#define UART_AUTODETECT 8
#define UART_AUTODETECT_NOWAIT 9
#define UART_WAKEUP_ON_RDA 10
#define UART_SEND_BREAK 13
////////////////////////////////////////////////////////////////// COMP
// Comparator Variables: C1OUT, C2OUT
// Constants used in setup_comparator() are:
//
#define NC_NC_NC_NC 0x00
#define NC_NC 0x00
//Pick one constant for COMP1
#define CP1_A0_A3 0x00090080
#define CP1_A1_A3 0x000A0081
#define CP1_B3_A3 0x00880082
#define CP1_B1_A3 0x00280083
#define CP1_A0_VREF 0x00010084
#define CP1_A1_VREF 0x00020085
#define CP1_B3_VREF 0x00800086
#define CP1_B1_VREF 0x00200087
//Optionally OR with one or both of the following
#define CP1_OUT_ON_A4 0x00000020
#define CP1_INVERT 0x00000010
#define CP1_ABSOLUTE_VREF 0x20000000
//OR with one constant for COMP2
#define CP2_A0_A2 0x00058000
#define CP2_A1_A2 0x00068100
#define CP2_B3_A2 0x00848200
#define CP2_B1_A2 0x00248300
#define CP2_A0_VREF 0x00018400
#define CP2_A1_VREF 0x00028500
#define CP2_B3_VREF 0x00808600
#define CP2_B1_VREF 0x00208700
//Optionally OR with one or both of the following
#define CP2_OUT_ON_A5 0x00002000
#define CP2_INVERT 0x00001000
#define CP2_ABSOLUTE_VREF 0x10000000
//Optionally OR with one or both of the following
#define CP2_T1_SYNC 0x01000000
#define CP2_T1_GATE 0x02000000
#bit C1OUT = 0x107.6
#bit C2OUT = 0x108.6
////////////////////////////////////////////////////////////////// VREF
// Constants used in setup_vref() are:
//
#define VREF_LOW 0xa0
#define VREF_HIGH 0x80
// Or (with |) the above with a number 0-15
////////////////////////////////////////////////////////////////// INTERNAL RC
// Constants used in setup_oscillator() are:
#define OSC_31KHZ 1
#define OSC_125KHZ 0x11
#define OSC_250KHZ 0x21
#define OSC_500KHZ 0x31
#define OSC_1MHZ 0x41
#define OSC_2MHZ 0x51
#define OSC_4MHZ 0x61
#define OSC_8MHZ 0x71
#define OSC_INTRC 1
#define OSC_NORMAL 0
// Result may be (ignore all other bits)
#define OSC_STATE_STABLE 4
#define OSC_31KHZ_STABLE 2
////////////////////////////////////////////////////////////////// ADC
// ADC Functions: SETUP_ADC(), SETUP_ADC_PORTS() (aka SETUP_PORT_A),
// SET_ADC_CHANNEL(), READ_ADC()
// Constants used for SETUP_ADC() are:
#define ADC_OFF 0 // ADC Off
#define ADC_CLOCK_DIV_2 0x100
#define ADC_CLOCK_DIV_8 0x40
#define ADC_CLOCK_DIV_32 0x80
#define ADC_CLOCK_INTERNAL 0xc0 // Internal 2-6us
// Constants used in SETUP_ADC_PORTS() are:
// First argument:
// OR together desired pins
#define sAN0 1 //| A0
#define sAN1 2 //| A1
#define sAN2 4 //| A2
#define sAN3 8 //| A3
#define sAN4 16 //| A5
#define sAN5 32 //| E0
#define sAN6 64 //| E1
#define sAN7 128 //| E2
#define sAN8 0x10000 //| B2
#define sAN9 0x20000 //| B3
#define sAN10 0x40000 //| B1
#define sAN11 0x80000 //| B4
#define sAN12 0x100000 //| B0
#define sAN13 0x200000 //| B5
#define NO_ANALOGS 0 // None
#define ALL_ANALOG 0x1F00FF // A0 A1 A2 A3 A5 E0 E1 E2 B0 B1 B2 B3 B4 B5
// Optional Second argument:
#define VSS_VDD 0x0000 //| Range 0-Vdd
#define VSS_VREF 0x1000 //| Range 0-Vref
#define VREF_VREF 0x3000 //| Range Vref-Vref
#define VREF_VDD 0x2000 //| Range Vref-Vdd
// Constants used in READ_ADC() are:
#define ADC_START_AND_READ 7 // This is the default if nothing is specified
#define ADC_START_ONLY 1
#define ADC_READ_ONLY 6
////////////////////////////////////////////////////////////////// INT
// Interrupt Functions: ENABLE_INTERRUPTS(), DISABLE_INTERRUPTS(),
// CLEAR_INTERRUPT(), INTERRUPT_ACTIVE(),
// EXT_INT_EDGE()
//
// Constants used in EXT_INT_EDGE() are:
#define L_TO_H 0x40
#define H_TO_L 0
// Constants used in ENABLE/DISABLE_INTERRUPTS() are:
#define GLOBAL 0x0BC0
#define PERIPH 0x0B40
#define INT_RTCC 0x000B20
#define INT_RB 0x01FF0B08
#define INT_EXT_L2H 0x50000B10
#define INT_EXT_H2L 0x60000B10
#define INT_EXT 0x000B10
#define INT_AD 0x008C40
#define INT_TBE 0x008C10
#define INT_RDA 0x008C20
#define INT_TIMER1 0x008C01
#define INT_TIMER2 0x008C02
#define INT_CCP1 0x008C04
#define INT_CCP2 0x008D01
#define INT_SSP 0x008C08
#define INT_BUSCOL 0x008D08
#define INT_EEPROM 0x008D10
#define INT_TIMER0 0x000B20
#define INT_OSC_FAIL 0x008D80
#define INT_COMP 0x008D20
#define INT_COMP2 0x008D40
#define INT_ULPWU 0x008D04
#define INT_RB0 0x0010B08
#define INT_RB1 0x0020B08
#define INT_RB2 0x0040B08
#define INT_RB3 0x0080B08
#define INT_RB4 0x0100B08
#define INT_RB5 0x0200B08
#define INT_RB6 0x0400B08
#define INT_RB7 0x0800B08
#list
******************** THU VIEN LCD ************
//#define lcd_light PIN_D0
#define lcd_RS PIN_D0
#define lcd_RW PIN_D1
#define lcd_E PIN_D2
#define lcd_B1 PIN_D4
#define lcd_B2 PIN_D5
#define lcd_B3 PIN_D6
#define lcd_B4 PIN_D7
#define dong_1 0x80
#define dong_2 0xC0
#define Xoa_lcd 0x01
//====================Cac ham dung ngoai thu vien==============
#separate void LCD_Init(); // khai bao cac tien to su dung LCD
#separate void LCD_Position(int x); // Di chuyen vi tri con tro
#separate void LCD_Char(int x); // Hien Thi 1 bien Char
#separate void LCD_Cmd(int x); // Gui 1 lenh len LCD
#separate void LCD_Pulse();
#separate void LCD_CGRAM();
//==========================Chuong trinh========================
#separate void LCD_Data (int x)
{
output_bit(lcd_B1, x & 0x01);
output_bit(lcd_B2, x & 0x02);
output_bit(lcd_B3, x & 0x04);
output_bit(lcd_B4, x & 0x08);
}
#separate void LCD_Pulse()
{
output_high(lcd_E);
delay_us ( 3); // was 10
output_low(lcd_E);
delay_us ( 150); // was 5
}
#separate void LCD_Position(int x)
{
LCD_Data(Swap(x) | 0x08);
LCD_Pulse();
LCD_Data(Swap(x));
LCD_Pulse();
}
#separate void LCD_Cmd (int x)
{
LCD_Data(Swap(x));
LCD_Pulse();
LCD_Data(Swap(x));
LCD_Pulse();
if (x == 0x01) delay_us(2000);
}
#separate void LCD_Char (int x)
{
output_high(lcd_RS);
LCD_Data(Swap(x));
LCD_Pulse();
LCD_Data(Swap(x));
LCD_Pulse();
output_low(lcd_RS);
}
#separate void LCD_Init ()
{
// output_high(lcd_open);
LCD_Data(0x00);
delay_ms(20); //cho cho VDD len >> 15ms
output_low (lcd_RS); //mac dinh la viet lenh
output_low (lcd_RW); //mac dinh la LCD nhan lenh
LCD_Data(0x03); //che do 4 bit
LCD_Pulse();
LCD_Pulse();
LCD_Pulse();
LCD_Data(0x02); //xoa man hinh
LCD_Pulse();
LCD_Cmd(0x2c);
LCD_Cmd(0x0c); //bat hien thi tat con tro
LCD_Cmd(0x06); //tang con tro
LCD_Cmd(0x01); //xoa man hinh hien thi
}
******** CHƯƠNG TRÌNH 1WIRE ********************
#ifndef ONE_WIRE_C
#define ONE_WIRE_C
/*
* One wire (1-wire) driver for CCS C compiler. Suitable for use with devices
* such as the DS18B20 1-wire digital temperature sensor.
*/
#define ONE_WIRE_PIN PIN_D3
/*
* onewire_reset()
* Description: Initiates the one wire bus.
*/
// OK if just using a single permanently connected device
void onewire_reset() {
output_low(ONE_WIRE_PIN); // pull the bus low for reset
delay_us(500);
output_float(ONE_WIRE_PIN); // float the bus high
delay_us(500); // wait-out remaining initialisation window
output_float(ONE_WIRE_PIN);
}
/*
* onewire_write(int8 data)
* Arguments: a byte of data.
* Description: writes a byte of data to the device.
*/
void onewire_write(int8 data) {
int8 count;
for(count = 0; count < 8; ++count) {
output_low(ONE_WIRE_PIN);
delay_us(2); // pull 1-wire low to initiate write time-slot.
output_bit(ONE_WIRE_PIN, shift_right(&data, 1, 0)); // set output bit on 1-wire
delay_us(60); // wait until end of write slot.
output_float(ONE_WIRE_PIN); // set 1-wire high again,
delay_us(2); // for more than 1us minimum.
}
}
/*
* onewire_read()
* Description: reads and returns a byte of data from the device.
*/
int onewire_read() {
int count, data;
for(count = 0; count < 8; ++count) {
output_low(ONE_WIRE_PIN);
delay_us(2); // pull 1-wire low to initiate read time-slot.
output_float(ONE_WIRE_PIN); // now let 1-wire float high,
delay_us(8); // let device state stabilise,
shift_right(&data, 1, input(ONE_WIRE_PIN)); // and load result.
delay_us(120); // wait until end of read slot.
}
return data;
}
#endif /*ONE_WIRE_C*/
********************* CHƯƠNG TRÌNH DS18B20**************88
#ifndef DS18B20_C
#define DS18B20_C
#include "1wire.c"
float ds18b20_read();
void ds18b20_configure(int8 TH, int8 TL, int8 config);
/*
* ds1820_read()
* Description: reads the ds18x20 device on the 1-wire bus and returns
* the temperature
*/
float ds18b20_read() {
int8 busy=0, temp1, temp2;
signed int16 temp3;
float result;
//ds1820_configure(0x00, 0x00, 0x00); //9 bit resolution
onewire_reset();
onewire_write(0xCC); //Skip ROM, address all devices
onewire_write(0x44); //Start temperature conversion
while(busy == 0) //Wait while busy (bus is low)
busy = onewire_read();
onewire_reset();
onewire_write(0xCC); //Skip ROM, address all devices
onewire_write(0xBE); //Read scratchpad
temp1 = onewire_read();
temp2 = onewire_read();
temp3 = make16(temp2, temp1);
//result = (float) temp3 / 2.0; //Calculation for DS18S20 with 0.5 deg C resolution
result = (float) temp3 / 16.0; //Calculation for DS18B20 with 0.1 deg C resolution
delay_ms(200); // ??????
return(result);
}
/*
* ds1820_configure(int8 TH, int8 LH, int8 config)
* Description: writes configuration data to the DS18x20 device
* Arguments: alarm trigger high, alarm trigger low, configuration
*/
void ds18b20_configure(int8 TH, int8 TL, int8 config) {
onewire_reset();
onewire_write(0xCC); //Skip ROM, address all devices
onewire_write(0x4E); //Write to scratchpad
onewire_write(TH);
onewire_write(TL);
onewire_write(config);
}
#endif /*DS1820_C*/
************************** CODE MAIN **************
#include <16F887.h>
#device *=16 adc=10
#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT,NOLVP, NOCPD, NOWRT
#use delay(clock=20000000)
#include <LCD4bit.h> // Thu vien ham cho LCD
#include "1wire.c"
#include "DS18B20.c"
float temperature;
//-----------------------------------------------------------------
void main()
{
LCD_Init();
//==== Khoi tao cho ngat ngoai============================================= ==
//enable_interrupts (INT_EXT);
//ext_int_edge(H_TO_L);
//enable_interrupts (GLOBAL);
//=========== Khoi tao che do cho bo ADC=====================================
LCD_Char("IT'S VERY HOT");
while(true)
{
temperature=ds18b20_read();
LCD_Cmd(0xC0);
Printf(LCD_Char,"%2.0f",temperature);
LCD_Char(223);
LCD_Char("C");
}
}
*************THU VIEN ***********************
//////// Standard Header file for the PIC16F887 device ////////////////
#device PIC16F887
#nolist
#define PIN_A0 40
#define PIN_A1 41
#define PIN_A2 42
#define PIN_A3 43
#define PIN_A4 44
#define PIN_A5 45
#define PIN_A6 46
#define PIN_A7 47
#define PIN_B0 48
#define PIN_B1 49
#define PIN_B2 50
#define PIN_B3 51
#define PIN_B4 52
#define PIN_B5 53
#define PIN_B6 54
#define PIN_B7 55
#define PIN_C0 56
#define PIN_C1 57
#define PIN_C2 58
#define PIN_C3 59
#define PIN_C4 60
#define PIN_C5 61
#define PIN_C6 62
#define PIN_C7 63
#define PIN_D0 64
#define PIN_D1 65
#define PIN_D2 66
#define PIN_D3 67
#define PIN_D4 68
#define PIN_D5 69
#define PIN_D6 70
#define PIN_D7 71
#define PIN_E0 72
#define PIN_E1 73
#define PIN_E2 74
#define PIN_E3 75
////////////////////////////////////////////////////////////////// Useful defines
#define FALSE 0
#define TRUE 1
#define BYTE int8
#define BOOLEAN int1
#define getc getch
#define fgetc getch
#define getchar getch
#define putc putchar
#define fputc putchar
#define fgets gets
#define fputs puts
////////////////////////////////////////////////////////////////// Control
// Control Functions: RESET_CPU(), SLEEP(), RESTART_CAUSE()
// Constants returned from RESTART_CAUSE() are:
#define WDT_FROM_SLEEP 3
#define WDT_TIMEOUT 11
#define MCLR_FROM_SLEEP 19
#define MCLR_FROM_RUN 27
#define NORMAL_POWER_UP 25
#define BROWNOUT_RESTART 26
////////////////////////////////////////////////////////////////// Timer 0
// Timer 0 (AKA RTCC)Functions: SETUP_COUNTERS() or SETUP_TIMER_0(),
// SET_TIMER0() or SET_RTCC(),
// GET_TIMER0() or GET_RTCC()
// Constants used for SETUP_TIMER_0() are:
#define T0_INTERNAL 0
#define T0_EXT_L_TO_H 32
#define T0_EXT_H_TO_L 48
#define T0_DIV_1 8
#define T0_DIV_2 0
#define T0_DIV_4 1
#define T0_DIV_8 2
#define T0_DIV_16 3
#define T0_DIV_32 4
#define T0_DIV_64 5
#define T0_DIV_128 6
#define T0_DIV_256 7
#define T0_8_BIT 0
#define RTCC_INTERNAL 0 // The following are provided for compatibility
#define RTCC_EXT_L_TO_H 32 // with older compiler versions
#define RTCC_EXT_H_TO_L 48
#define RTCC_DIV_1 8
#define RTCC_DIV_2 0
#define RTCC_DIV_4 1
#define RTCC_DIV_8 2
#define RTCC_DIV_16 3
#define RTCC_DIV_32 4
#define RTCC_DIV_64 5
#define RTCC_DIV_128 6
#define RTCC_DIV_256 7
#define RTCC_8_BIT 0
// Constants used for SETUP_COUNTERS() are the above
// constants for the 1st param and the following for
// the 2nd param:
////////////////////////////////////////////////////////////////// WDT
// Watch Dog Timer Functions: SETUP_WDT() or SETUP_COUNTERS() (see above)
// RESTART_WDT()
// WDT base is 18ms
//
#define WDT_18MS 8
#define WDT_36MS 9
#define WDT_72MS 10
#define WDT_144MS 11
#define WDT_288MS 12
#define WDT_576MS 13
#define WDT_1152MS 14
#define WDT_2304MS 15
// One of the following may be OR'ed in with the above:
#define WDT_ON 0x4100
#define WDT_OFF 0
#define WDT_DIV_16 0x100
#define WDT_DIV_8 0x300
#define WDT_DIV_4 0x500
#define WDT_DIV_2 0x700
#define WDT_TIMES_1 0x900 // Default
#define WDT_TIMES_2 0xB00
#define WDT_TIMES_4 0xD00
#define WDT_TIMES_8 0xF00
#define WDT_TIMES_16 0x1100
#define WDT_TIMES_32 0x1300
#define WDT_TIMES_64 0x1500
#define WDT_TIMES_128 0x1700
////////////////////////////////////////////////////////////////// Timer 1
// Timer 1 Functions: SETUP_TIMER_1, GET_TIMER1, SET_TIMER1
// Constants used for SETUP_TIMER_1() are:
// (or (via |) together constants from each group)
#define T1_DISABLED 0
#define T1_INTERNAL 5
#define T1_EXTERNAL 7
#define T1_EXTERNAL_SYNC 3
#define T1_CLK_OUT 8
#define T1_DIV_BY_1 0
#define T1_DIV_BY_2 0x10
#define T1_DIV_BY_4 0x20
#define T1_DIV_BY_8 0x30
#define T1_GATE 0x40
#define T1_GATE_INVERTED 0xC0
////////////////////////////////////////////////////////////////// Timer 2
// Timer 2 Functions: SETUP_TIMER_2, GET_TIMER2, SET_TIMER2
// Constants used for SETUP_TIMER_2() are:
#define T2_DISABLED 0
#define T2_DIV_BY_1 4
#define T2_DIV_BY_4 5
#define T2_DIV_BY_16 6
////////////////////////////////////////////////////////////////// CCP
// CCP Functions: SETUP_CCPx, SET_PWMx_DUTY
// CCP Variables: CCP_x, CCP_x_LOW, CCP_x_HIGH
// Constants used for SETUP_CCPx() are:
#define CCP_OFF 0
#define CCP_CAPTURE_FE 4
#define CCP_CAPTURE_RE 5
#define CCP_CAPTURE_DIV_4 6
#define CCP_CAPTURE_DIV_16 7
#define CCP_COMPARE_SET_ON_MATCH 8
#define CCP_COMPARE_CLR_ON_MATCH 9
#define CCP_COMPARE_INT 0xA
#define CCP_COMPARE_RESET_TIMER 0xB
#define CCP_PWM 0xC
#define CCP_PWM_PLUS_1 0x1c
#define CCP_PWM_PLUS_2 0x2c
#define CCP_PWM_PLUS_3 0x3c
#word CCP_1 = getenv("SFR:CCPR1L")
#byte CCP_1_LOW = getenv("SFR:CCPR1L")
#byte CCP_1_HIGH = getenv("SFR:CCPR1H")
// The following should be used with the ECCP unit only (or these in)
#define CCP_PWM_H_H 0x0c
#define CCP_PWM_H_L 0x0d
#define CCP_PWM_L_H 0x0e
#define CCP_PWM_L_L 0x0f
#define CCP_PWM_FULL_BRIDGE 0x40
#define CCP_PWM_FULL_BRIDGE_REV 0xC0
#define CCP_PWM_HALF_BRIDGE 0x80
#define CCP_SHUTDOWN_ON_COMP1 0x100000
#define CCP_SHUTDOWN_ON_COMP2 0x200000
#define CCP_SHUTDOWN_ON_COMP 0x300000
#define CCP_SHUTDOWN_ON_INT0 0x400000
#define CCP_SHUTDOWN_ON_COMP1_INT0 0x500000
#define CCP_SHUTDOWN_ON_COMP2_INT0 0x600000
#define CCP_SHUTDOWN_ON_COMP_INT0 0x700000
#define CCP_SHUTDOWN_AC_L 0x000000
#define CCP_SHUTDOWN_AC_H 0x040000
#define CCP_SHUTDOWN_AC_F 0x080000
#define CCP_SHUTDOWN_BD_L 0x000000
#define CCP_SHUTDOWN_BD_H 0x010000
#define CCP_SHUTDOWN_BD_F 0x020000
#define CCP_SHUTDOWN_RESTART 0x80000000
#define CCP_PULSE_STEERING_A 0x01000000
#define CCP_PULSE_STEERING_B 0x02000000
#define CCP_PULSE_STEERING_C 0x04000000
#define CCP_PULSE_STEERING_D 0x08000000
#define CCP_PULSE_STEERING_SYNC 0x10000000
#word CCP_2 = getenv("SFR:CCPR2L")
#byte CCP_2_LOW = getenv("SFR:CCPR2L")
#byte CCP_2_HIGH = getenv("SFR:CCPR2H")
////////////////////////////////////////////////////////////////// SPI
// SPI Functions: SETUP_SPI, SPI_WRITE, SPI_READ, SPI_DATA_IN
// Constants used in SETUP_SPI() are:
#define SPI_MASTER 0x20
#define SPI_SLAVE 0x24
#define SPI_SCK_IDLE_HIGH 0x10
#define SPI_SCK_IDLE_LOW 0x00
#define SPI_CLK_DIV_4 0x00
#define SPI_CLK_DIV_16 0x01
#define SPI_CLK_DIV_64 0x02
#define SPI_CLK_T2 0x03
#define SPI_SS_DISABLED 0x01
#define SPI_XMIT_L_TO_H 0x4000
#define SPI_XMIT_H_TO_L 0x0000
#define SPI_SAMPLE_AT_MIDDLE 0x0000
#define SPI_SAMPLE_AT_END 0x8000
//The following are provided for compatibility
#define SPI_L_TO_H SPI_SCK_IDLE_LOW
#define SPI_H_TO_L SPI_SCK_IDLE_HIGH
////////////////////////////////////////////////////////////////// UART
// Constants used in setup_uart() are:
// FALSE - Turn UART off
// TRUE - Turn UART on
#define UART_ADDRESS 2
#define UART_DATA 4
#define UART_AUTODETECT 8
#define UART_AUTODETECT_NOWAIT 9
#define UART_WAKEUP_ON_RDA 10
#define UART_SEND_BREAK 13
////////////////////////////////////////////////////////////////// COMP
// Comparator Variables: C1OUT, C2OUT
// Constants used in setup_comparator() are:
//
#define NC_NC_NC_NC 0x00
#define NC_NC 0x00
//Pick one constant for COMP1
#define CP1_A0_A3 0x00090080
#define CP1_A1_A3 0x000A0081
#define CP1_B3_A3 0x00880082
#define CP1_B1_A3 0x00280083
#define CP1_A0_VREF 0x00010084
#define CP1_A1_VREF 0x00020085
#define CP1_B3_VREF 0x00800086
#define CP1_B1_VREF 0x00200087
//Optionally OR with one or both of the following
#define CP1_OUT_ON_A4 0x00000020
#define CP1_INVERT 0x00000010
#define CP1_ABSOLUTE_VREF 0x20000000
//OR with one constant for COMP2
#define CP2_A0_A2 0x00058000
#define CP2_A1_A2 0x00068100
#define CP2_B3_A2 0x00848200
#define CP2_B1_A2 0x00248300
#define CP2_A0_VREF 0x00018400
#define CP2_A1_VREF 0x00028500
#define CP2_B3_VREF 0x00808600
#define CP2_B1_VREF 0x00208700
//Optionally OR with one or both of the following
#define CP2_OUT_ON_A5 0x00002000
#define CP2_INVERT 0x00001000
#define CP2_ABSOLUTE_VREF 0x10000000
//Optionally OR with one or both of the following
#define CP2_T1_SYNC 0x01000000
#define CP2_T1_GATE 0x02000000
#bit C1OUT = 0x107.6
#bit C2OUT = 0x108.6
////////////////////////////////////////////////////////////////// VREF
// Constants used in setup_vref() are:
//
#define VREF_LOW 0xa0
#define VREF_HIGH 0x80
// Or (with |) the above with a number 0-15
////////////////////////////////////////////////////////////////// INTERNAL RC
// Constants used in setup_oscillator() are:
#define OSC_31KHZ 1
#define OSC_125KHZ 0x11
#define OSC_250KHZ 0x21
#define OSC_500KHZ 0x31
#define OSC_1MHZ 0x41
#define OSC_2MHZ 0x51
#define OSC_4MHZ 0x61
#define OSC_8MHZ 0x71
#define OSC_INTRC 1
#define OSC_NORMAL 0
// Result may be (ignore all other bits)
#define OSC_STATE_STABLE 4
#define OSC_31KHZ_STABLE 2
////////////////////////////////////////////////////////////////// ADC
// ADC Functions: SETUP_ADC(), SETUP_ADC_PORTS() (aka SETUP_PORT_A),
// SET_ADC_CHANNEL(), READ_ADC()
// Constants used for SETUP_ADC() are:
#define ADC_OFF 0 // ADC Off
#define ADC_CLOCK_DIV_2 0x100
#define ADC_CLOCK_DIV_8 0x40
#define ADC_CLOCK_DIV_32 0x80
#define ADC_CLOCK_INTERNAL 0xc0 // Internal 2-6us
// Constants used in SETUP_ADC_PORTS() are:
// First argument:
// OR together desired pins
#define sAN0 1 //| A0
#define sAN1 2 //| A1
#define sAN2 4 //| A2
#define sAN3 8 //| A3
#define sAN4 16 //| A5
#define sAN5 32 //| E0
#define sAN6 64 //| E1
#define sAN7 128 //| E2
#define sAN8 0x10000 //| B2
#define sAN9 0x20000 //| B3
#define sAN10 0x40000 //| B1
#define sAN11 0x80000 //| B4
#define sAN12 0x100000 //| B0
#define sAN13 0x200000 //| B5
#define NO_ANALOGS 0 // None
#define ALL_ANALOG 0x1F00FF // A0 A1 A2 A3 A5 E0 E1 E2 B0 B1 B2 B3 B4 B5
// Optional Second argument:
#define VSS_VDD 0x0000 //| Range 0-Vdd
#define VSS_VREF 0x1000 //| Range 0-Vref
#define VREF_VREF 0x3000 //| Range Vref-Vref
#define VREF_VDD 0x2000 //| Range Vref-Vdd
// Constants used in READ_ADC() are:
#define ADC_START_AND_READ 7 // This is the default if nothing is specified
#define ADC_START_ONLY 1
#define ADC_READ_ONLY 6
////////////////////////////////////////////////////////////////// INT
// Interrupt Functions: ENABLE_INTERRUPTS(), DISABLE_INTERRUPTS(),
// CLEAR_INTERRUPT(), INTERRUPT_ACTIVE(),
// EXT_INT_EDGE()
//
// Constants used in EXT_INT_EDGE() are:
#define L_TO_H 0x40
#define H_TO_L 0
// Constants used in ENABLE/DISABLE_INTERRUPTS() are:
#define GLOBAL 0x0BC0
#define PERIPH 0x0B40
#define INT_RTCC 0x000B20
#define INT_RB 0x01FF0B08
#define INT_EXT_L2H 0x50000B10
#define INT_EXT_H2L 0x60000B10
#define INT_EXT 0x000B10
#define INT_AD 0x008C40
#define INT_TBE 0x008C10
#define INT_RDA 0x008C20
#define INT_TIMER1 0x008C01
#define INT_TIMER2 0x008C02
#define INT_CCP1 0x008C04
#define INT_CCP2 0x008D01
#define INT_SSP 0x008C08
#define INT_BUSCOL 0x008D08
#define INT_EEPROM 0x008D10
#define INT_TIMER0 0x000B20
#define INT_OSC_FAIL 0x008D80
#define INT_COMP 0x008D20
#define INT_COMP2 0x008D40
#define INT_ULPWU 0x008D04
#define INT_RB0 0x0010B08
#define INT_RB1 0x0020B08
#define INT_RB2 0x0040B08
#define INT_RB3 0x0080B08
#define INT_RB4 0x0100B08
#define INT_RB5 0x0200B08
#define INT_RB6 0x0400B08
#define INT_RB7 0x0800B08
#list
******************** THU VIEN LCD ************
//#define lcd_light PIN_D0
#define lcd_RS PIN_D0
#define lcd_RW PIN_D1
#define lcd_E PIN_D2
#define lcd_B1 PIN_D4
#define lcd_B2 PIN_D5
#define lcd_B3 PIN_D6
#define lcd_B4 PIN_D7
#define dong_1 0x80
#define dong_2 0xC0
#define Xoa_lcd 0x01
//====================Cac ham dung ngoai thu vien==============
#separate void LCD_Init(); // khai bao cac tien to su dung LCD
#separate void LCD_Position(int x); // Di chuyen vi tri con tro
#separate void LCD_Char(int x); // Hien Thi 1 bien Char
#separate void LCD_Cmd(int x); // Gui 1 lenh len LCD
#separate void LCD_Pulse();
#separate void LCD_CGRAM();
//==========================Chuong trinh========================
#separate void LCD_Data (int x)
{
output_bit(lcd_B1, x & 0x01);
output_bit(lcd_B2, x & 0x02);
output_bit(lcd_B3, x & 0x04);
output_bit(lcd_B4, x & 0x08);
}
#separate void LCD_Pulse()
{
output_high(lcd_E);
delay_us ( 3); // was 10
output_low(lcd_E);
delay_us ( 150); // was 5
}
#separate void LCD_Position(int x)
{
LCD_Data(Swap(x) | 0x08);
LCD_Pulse();
LCD_Data(Swap(x));
LCD_Pulse();
}
#separate void LCD_Cmd (int x)
{
LCD_Data(Swap(x));
LCD_Pulse();
LCD_Data(Swap(x));
LCD_Pulse();
if (x == 0x01) delay_us(2000);
}
#separate void LCD_Char (int x)
{
output_high(lcd_RS);
LCD_Data(Swap(x));
LCD_Pulse();
LCD_Data(Swap(x));
LCD_Pulse();
output_low(lcd_RS);
}
#separate void LCD_Init ()
{
// output_high(lcd_open);
LCD_Data(0x00);
delay_ms(20); //cho cho VDD len >> 15ms
output_low (lcd_RS); //mac dinh la viet lenh
output_low (lcd_RW); //mac dinh la LCD nhan lenh
LCD_Data(0x03); //che do 4 bit
LCD_Pulse();
LCD_Pulse();
LCD_Pulse();
LCD_Data(0x02); //xoa man hinh
LCD_Pulse();
LCD_Cmd(0x2c);
LCD_Cmd(0x0c); //bat hien thi tat con tro
LCD_Cmd(0x06); //tang con tro
LCD_Cmd(0x01); //xoa man hinh hien thi
}
******** CHƯƠNG TRÌNH 1WIRE ********************
#ifndef ONE_WIRE_C
#define ONE_WIRE_C
/*
* One wire (1-wire) driver for CCS C compiler. Suitable for use with devices
* such as the DS18B20 1-wire digital temperature sensor.
*/
#define ONE_WIRE_PIN PIN_D3
/*
* onewire_reset()
* Description: Initiates the one wire bus.
*/
// OK if just using a single permanently connected device
void onewire_reset() {
output_low(ONE_WIRE_PIN); // pull the bus low for reset
delay_us(500);
output_float(ONE_WIRE_PIN); // float the bus high
delay_us(500); // wait-out remaining initialisation window
output_float(ONE_WIRE_PIN);
}
/*
* onewire_write(int8 data)
* Arguments: a byte of data.
* Description: writes a byte of data to the device.
*/
void onewire_write(int8 data) {
int8 count;
for(count = 0; count < 8; ++count) {
output_low(ONE_WIRE_PIN);
delay_us(2); // pull 1-wire low to initiate write time-slot.
output_bit(ONE_WIRE_PIN, shift_right(&data, 1, 0)); // set output bit on 1-wire
delay_us(60); // wait until end of write slot.
output_float(ONE_WIRE_PIN); // set 1-wire high again,
delay_us(2); // for more than 1us minimum.
}
}
/*
* onewire_read()
* Description: reads and returns a byte of data from the device.
*/
int onewire_read() {
int count, data;
for(count = 0; count < 8; ++count) {
output_low(ONE_WIRE_PIN);
delay_us(2); // pull 1-wire low to initiate read time-slot.
output_float(ONE_WIRE_PIN); // now let 1-wire float high,
delay_us(8); // let device state stabilise,
shift_right(&data, 1, input(ONE_WIRE_PIN)); // and load result.
delay_us(120); // wait until end of read slot.
}
return data;
}
#endif /*ONE_WIRE_C*/
********************* CHƯƠNG TRÌNH DS18B20**************88
#ifndef DS18B20_C
#define DS18B20_C
#include "1wire.c"
float ds18b20_read();
void ds18b20_configure(int8 TH, int8 TL, int8 config);
/*
* ds1820_read()
* Description: reads the ds18x20 device on the 1-wire bus and returns
* the temperature
*/
float ds18b20_read() {
int8 busy=0, temp1, temp2;
signed int16 temp3;
float result;
//ds1820_configure(0x00, 0x00, 0x00); //9 bit resolution
onewire_reset();
onewire_write(0xCC); //Skip ROM, address all devices
onewire_write(0x44); //Start temperature conversion
while(busy == 0) //Wait while busy (bus is low)
busy = onewire_read();
onewire_reset();
onewire_write(0xCC); //Skip ROM, address all devices
onewire_write(0xBE); //Read scratchpad
temp1 = onewire_read();
temp2 = onewire_read();
temp3 = make16(temp2, temp1);
//result = (float) temp3 / 2.0; //Calculation for DS18S20 with 0.5 deg C resolution
result = (float) temp3 / 16.0; //Calculation for DS18B20 with 0.1 deg C resolution
delay_ms(200); // ??????
return(result);
}
/*
* ds1820_configure(int8 TH, int8 LH, int8 config)
* Description: writes configuration data to the DS18x20 device
* Arguments: alarm trigger high, alarm trigger low, configuration
*/
void ds18b20_configure(int8 TH, int8 TL, int8 config) {
onewire_reset();
onewire_write(0xCC); //Skip ROM, address all devices
onewire_write(0x4E); //Write to scratchpad
onewire_write(TH);
onewire_write(TL);
onewire_write(config);
}
#endif /*DS1820_C*/
Comment