Vous êtes sur la page 1sur 68

PICDEM 2 PLUS SAMPLE CODE

2004 -2008 Sure Electronics Inc.

DB-DP113_Ver1.0

PICDEM 2 PLUS SAMPLE CODE


Sample 1. Light LEDs Associated With PORTB ................................................. 2 Sample 2. Make the Buzzer Beep ........................................................................ 3 Sample 3. Read Temperature from Built-in IIC Temperature Sensor ............... 4 Sample 4. How to Read and Write to the Data EEPROM Memory .................... 7 Sample 5. Transmit Data through the USART of PIC18F4520 ........................ 10 Sample 6. How to Display Information on 7-segment LEDs ............................11 Sample 7. How to Display Information on HD44780 LCD Module .................. 13 Sample 8. LED Mode Code ................................................................................. 30 Sample 9. Display Information on the LCD Module. ........................................ 40

2004 -2008 Sure Electronics Inc.

DB-DP113_Ver1.0_Page1

PICDEM 2 PLUS SAMPLE CODE

Sample 1. Light LEDs Associated With PORTB

This Sample will show you how to illuminate LED associated with PORTB of PIC18F4520 microcontroller. LEDs are connected to PORTB1-3, when those pins are set to low, corresponding LED would be illuminated. #include <p18f4520.h> #include <delays.h> #pragma config OSC = HSPLL #pragma config PWRT = OFF #pragma config BOREN = OFF #pragma config WDT = OFF #pragma config MCLRE = ON #pragma config PBADEN = OFF #pragma config LVP = OFF // Initializtion void init(void) { CMCON=0b00000111; TRISA=0b00010000; TRISB=0b00000001; TRISC=0b00000000; TRISD=0b00000000; TRISE=0b00001000; ADCON1=0b00001111; } // Main Program void main( void ) { init(); while(1) { PORTBbits.RB1=0; Delay1KTCYx(255); PORTBbits.RB1=1; Delay1KTCYx(255); PORTBbits.RB2=0; Delay1KTCYx(255); PORTBbits.RB2=1; Delay1KTCYx(255);
DB-DP113_Ver2.0_Page2 2004 -2008 Sure Electronics Inc.

// High-Speed Crystal/Resonator //with PLL enabled

// Close Comparator

// Configure Digital Channel

// Initialize Microchip

PICDEM 2 PLUS SAMPLE CODE

PORTBbits.RB3=0; Delay1KTCYx(255); PORTBbits.RB3=1; Delay1KTCYx(255); } }

Sample 2. Make the Buzzer Beep

In this Sample, we will show you how to make the speaker that connected to PORTC2 (CCP1) pin buzz. Before starting this test, you should first connect a passive speaker to J2. The speaker is connected to the collector of a NPN transistor built in ULN2003 chip. Base of the ULN2003 chip is driven by PORTC2 (CCP1 Pin). When a PWM wave is applied on the PORTC2 pin, the speaker will start buzzing. #include <p18f4520.h> #include <delays.h> #pragma config OSC = HS #pragma config PWRT = OFF #pragma config BOREN = OFF #pragma config WDT = OFF #pragma config MCLRE = ON #pragma config PBADEN = OFF #pragma config LVP = OFF void init(void); void PWM(unsigned char i); // Initializtion void init(void) { CMCON=0b00000111; TRISA=0b00010000; TRISB=0b00000001; TRISC=0b00000000; TRISD=0b00000000; TRISE=0b00001000; ADCON1=0b00001111; } // Set PWM Mode void PWM(unsigned char i)
2004 -2008 Sure Electronics Inc.

// Close Comparator

// Configure Digital Channel

DB-DP113_Ver2.0_Page3

PICDEM 2 PLUS SAMPLE CODE


{ CCP1CON=0b00001100; T2CONbits.TMR2ON = 0; T2CONbits.T2OUTPS3 = 1; T2CONbits.T2OUTPS2 = 1; T2CONbits.T2OUTPS1 = 1; T2CONbits.T2OUTPS0 = 1; T2CONbits.T2CKPS1 = 1; T2CONbits.T2CKPS1 = 1; PR2 = 255; TRISCbits.TRISC2=0; T2CONbits.TMR2ON = 1; CCPR1L = 25*i; } // Main Programmer void main( void ) { init(); while(1) { PWM(4); Delay1KTCYx(255); PWM(0); Delay1KTCYx(255); } }

// Initializtion

//Buzzer Beep

Sample 3. Read Temperature from Built-in IIC Temperature Sensor

This Sample will show you how to read temperature value from LM75A temperature sensor via IIC interface of PIC18F4520 control microchip. In this demo code, only IIC operation of PIC18F4520 control microchip is shown. For hardware IIC chip, you could change sw_i2c.h to hw_i2c.h and modify this program. LM75A had been connected to PORTC3 and PORTC4. When correct time sequence has been applied on those 2 pins, temperature can be obtained from LM75A temperature sensor. #include <p18f4520.h> #include <sw_i2c.h> #pragma config OSC = HS #pragma config PWRT = OFF #pragma config BOREN = OFF
DB-DP113_Ver2.0_Page4 2004 -2008 Sure Electronics Inc.

PICDEM 2 PLUS SAMPLE CODE


#pragma config WDT = OFF #pragma config MCLRE = ON #pragma config PBADEN = OFF #pragma config LVP = OFF unsigned char i2c_var; unsigned int cvalue,fvalue; unsigned char cent_buf[5],fahr_buf[5]; // Initializtion void init(void) { CMCON=0b00000111; TRISA=0b00010000; TRISB=0b00000001; TRISC=0b00000000; TRISD=0b00000000; TRISE=0b00001000; ADCON1=0b00001111; } void LM75_init(void) { SWStartI2C(); i2c_var = SWPutcI2C(0x90); SWAckI2C(); i2c_var = SWPutcI2C(0x01); SWAckI2C(); i2c_var = SWPutcI2C(0x18); SWAckI2C(); SWStopI2C(); } void LM75_temperature(void) { unsigned char tptr[2]; unsigned int temp_H,temp_L; SWStartI2C();
2004 -2008 Sure Electronics Inc. DB-DP113_Ver2.0_Page5

// Memory Centigrade and Fahrenheit value // Centigrade and Fahrenheit value array

// Close Comparator

// Configure Digital Channel

// Temperature Sensor Initializtion

// Control Byte // Configure Register // Configure Byte

PICDEM 2 PLUS SAMPLE CODE


i2c_var = SWPutcI2C(0x90); SWAckI2C(); i2c_var = SWPutcI2C(0x00); SWAckI2C(); SWRestartI2C(); i2c_var = SWPutcI2C(0x91); SWAckI2C(); i2c_var = SWGetsI2C(tptr, 2); SWStopI2C(); temp_H=tptr[0]; temp_L=tptr[1]; // Compute Centigrade cvalue=(temp_H<<8)|temp_L; cent_buf[0]=' '; if(cvalue&0x80==1) { cvalue=~cvalue+1; cent_buf[0]='-'; } cvalue=cvalue>>5; cvalue=cvalue * 1.25; cent_buf[1]=cvalue/100+48; cent_buf[2]=(cvalue/10)%10+48; cent_buf[3]='.'; cent_buf[4]=cvalue%10+48; cent_buf[5]='\0'; // Compute Fahrenheit fvalue=((cvalue*9)/5)+32; fahr_buf[0]=' '; if(fvalue&0x80==1) { fvalue=~fvalue+1; fahr_buf[0]='-'; } fahr_buf[1]=fvalue/100+48; fahr_buf[2]=(fvalue/10)%10+48; fahr_buf[3]='.';
DB-DP113_Ver2.0_Page6

// Control Byte // Data Address

// Control Byte // Read Temperature Value

// High Bits // Low Bits

//Calculate Base Complement

//Calculate Base Complement

2004 -2008 Sure Electronics Inc.

PICDEM 2 PLUS SAMPLE CODE


fahr_buf[4]=fvalue%10+48; fahr_buf[5]='\0'; } // Main Programmer void main( void ) { init(); LM75_init(); while(1) { LM75_temperature(); } }

// Initialize Control Microchip // Temperature Sensor Initializtion

// Read Temperature Value

Sample 4. How to Read and Write to the Data EEPROM Memory

This demo code will show you how to read and write data EEPROM memory microchip 24C04 via PIC18F4520 control microchip. 24C04 EEPROM memory microchip is connected to PORTC3 and PORTC4 of PIC18F4520 microchip, when correct time sequence is applied on 24C04, it could be read or written to. #include <p18f4520.h> #include <sw_i2c.h> #include <delays.h> #pragma config OSC = HS #pragma config PWRT = OFF #pragma config BOREN = OFF #pragma config WDT = OFF #pragma config MCLRE = ON #pragma config PBADEN = OFF #pragma config LVP = OFF unsigned char i2c_var; unsigned char wr_data[]={0x0a,0x0b,0x0c,0x0d}; unsigned char rd_data[4]; // Initializtion
2004 -2008 Sure Electronics Inc. DB-DP113_Ver2.0_Page7

PICDEM 2 PLUS SAMPLE CODE


void init(void) { CMCON=0b00000111; TRISA=0b00010000; TRISB=0b00000001; TRISC=0b00000000; TRISD=0b00000000; TRISE=0b00001000; ADCON1=0b00001111; }

// Close Comparator // RA4 is for Switch SW1 Input // RB0 is for Switch SW2 Input

// RE3 is for Switch SW3 Input // Configure Digital Channel

// Write data void byte_write(unsigned char adr,unsigned char data) { SWStartI2C(); i2c_var = SWPutcI2C(0xA0); // Control Byte SWAckI2C(); i2c_var = SWPutcI2C(adr); // Word address SWAckI2C(); i2c_var = SWPutcI2C(data); // Write Data SWAckI2C(); SWStopI2C(); } // Read data void byte_read(unsigned char adr) { SWStartI2C(); i2c_var = SWPutcI2C( 0xA0 ); SWAckI2C(); i2c_var = SWPutcI2C(adr); SWAckI2C(); SWRestartI2C(); i2c_var = SWPutcI2C( 0xA1 ); SWAckI2C(); i2c_var = SWGetcI2C(); SWStopI2C(); }

// Control Byte // Word Address

// Control Byte // Get Data

// Write string void page_write(unsigned char adr,unsigned char wdata[]) { SWStartI2C(); i2c_var = SWPutcI2C(0xA0); // Control Byte SWAckI2C(); i2c_var = SWPutcI2C(adr); // Word Address SWAckI2C(); i2c_var = SWPutsI2C(wdata); // Get Data SWStopI2C(); }
DB-DP113_Ver2.0_Page8 2004 -2008 Sure Electronics Inc.

PICDEM 2 PLUS SAMPLE CODE

// Read string void sequential_read(unsigned char adr,unsigned char rdata[],unsigned char len) { SWStartI2C(); i2c_var = SWPutcI2C( 0xA0 ); // Control Byte SWAckI2C(); i2c_var = SWPutcI2C(adr); // Word Address SWAckI2C(); SWRestartI2C(); i2c_var = SWPutcI2C( 0xA1 ); // Control Byte SWAckI2C(); i2c_var = SWGetsI2C(rdata,len); // Get Data SWStopI2C(); } // Inquiries confirmed void ack_poll( void ) { SWStartI2C(); i2c_var = SWPutcI2C( 0xA0 ); while( SWAckI2C() ) { SWRestartI2C(); i2c_var = SWPutcI2C(0xA0); } SWStopI2C(); } // Main Programmer void main( void ) { init();

// Control Byte

// Write Data

// Initialize Control Microchip

while(1) { ack_poll(); page_write(0x02,wr_data); ack_poll(); Nop(); sequential_read(0x02,rd_data,4); Nop(); } }

2004 -2008 Sure Electronics Inc.

DB-DP113_Ver2.0_Page9

PICDEM 2 PLUS SAMPLE CODE Sample 5. Transmit Data through the USART of PIC18F4520

This Sample will show you how to transmit data through USART of PIC18F4520 microcontroller. User must configure Hyper Terminal or other terminal software in PC correctly, and then connect it to this development board. A single-chip USB to UART Bridge CP2102 is connected to PORTC6/TX, PORTC 7/ RX of PIC18F4520 microcontroller. #include <p18f4520.h> #include <sw_i2c.h> #include <delays.h> #include <usart.h> #pragma config OSC = HS #pragma config PWRT = OFF #pragma config BOREN = OFF #pragma config WDT = OFF #pragma config MCLRE = ON #pragma config PBADEN = OFF #pragma config LVP = OFF // Initializtion void init(void) { CMCON=0b00000111; TRISA=0b00010000; TRISB=0b00000001; TRISC=0b00000000; TRISD=0b00000000; TRISE=0b00001000; ADCON1=0b00001111; SPBRG=38; BAUDCONbits.BRG16=0; TXSTAbits.BRGH=0; TXSTAbits.SYNC=0; RCSTAbits.SPEN=1; TXSTAbits.TX9=0; TXSTAbits.TXEN=1; } // Main Programmer void main( void ) { union USART USART_Status; char wr_data[]={"world"};
DB-DP113_Ver2.0_Page10 2004 -2008 Sure Electronics Inc.

// Close Comparator // RA4 is for Switch SW1 Input // RB0 is for Switch SW2 Input

// RE3 is for Switch SW3 Input // Configure Digital Channel // Baud Rate 4800bps // Choose 8-bit Baud Rate Generator // High Baud Rate // Asynchronous Mode // Enable Serial Port // Transmit 8-bit data // Enable Transmission

PICDEM 2 PLUS SAMPLE CODE


char rd_data[7]; init(); WriteUSART('Q'); putsUSART(wr_data); while(!PIR1bits.TXIF) continue; } // Initializtion Program // Transmit Q // Transmit Data

Sample 6. How to Display Information on 7-segment LEDs

This demonstration board shows how to display information on 7-segment LEDs through GPIOs of PIC18F4520 microchip. PORTD is connected to a-h pins of 7segment LEDs, and PORTB4-7 is connected to ULN2003 chip, that drive 4 cathodes of those LEDs. Remove the LCD panel above the LED segments. Transmit data via PORTD and enable one of the cathodes, then the LED segments will display a digit letter. If switch of cathode is quick enough, it will display 4 digits and looks seamlessly. #include <p18f4520.h> #include <delays.h> #pragma config OSC = HSPLL #pragma config PWRT = OFF #pragma config BOREN = OFF #pragma config WDT = OFF #pragma config MCLRE = ON #pragma config PBADEN = OFF #pragma config LVP = OFF #define LED0 PORTDbits.RD0 #define LED1 PORTDbits.RD1 #define LED2 PORTDbits.RD2 #define LED3 PORTDbits.RD3 #define LED4 PORTDbits.RD4 #define LED5 PORTDbits.RD5 #define LED6 PORTDbits.RD6
2004 -2008 Sure Electronics Inc. DB-DP113_Ver2.0_Page11

PICDEM 2 PLUS SAMPLE CODE


#define LED7 PORTDbits.RD7 #define LEDbuf0 PORTBbits.RB4 #define LEDbuf1 PORTBbits.RB5 #define LEDbuf2 PORTBbits.RB6 #define LEDbuf3 PORTBbits.RB7 // Initializtion void init(void) { CMCON=0b00000111; TRISA=0b00010000; TRISB=0b00000001; TRISC=0b00000000; TRISD=0b00000000; TRISE=0b00001000; ADCON1=0b00001111; } // Main Programmer void main( void ) { init(); while(1) { LEDbuf0=0; LEDbuf1=0; LEDbuf2=0; LEDbuf3=1; LED0=0; LED1=1; LED2=1; LED3=0; LED4=0; LED5=0; LED6=0; LED7=0; Delay100TCYx(2); LEDbuf0=0; LEDbuf1=0; LEDbuf2=1; LEDbuf3=0; LED0=1; LED1=1; LED2=0; LED3=1; LED4=1; LED5=0; LED6=1;
DB-DP113_Ver2.0_Page12 2004 -2008 Sure Electronics Inc.

// Close Comparator // RA4 is for Switch SW1 Input // RB0 is for Switch SW2 Input

// RE3 is for Switch SW3 Input // Configure Digital Channel

PICDEM 2 PLUS SAMPLE CODE


LED7=0; Delay100TCYx(2); LEDbuf0=0; LEDbuf1=1; LEDbuf2=0; LEDbuf3=0; LED0=1; LED1=1; LED2=1; LED3=1; LED4=0; LED5=0; LED6=1; LED7=0; Delay100TCYx(2); LEDbuf0=1; LEDbuf1=0; LEDbuf2=0; LEDbuf3=0; LED0=0; LED1=1; LED2=1; LED3=0; LED4=0; LED5=1; LED6=1; LED7=0; Delay100TCYx(2); } }

Sample 7. How to Display Information on HD44780 LCD Module

This Sample will show you how to display information on HD44780 compatible LCD Module. The LCD module is connected to PORTD (as data port), PORTA1 (E signal of LCD module), PORTA2 (RW signal of LCD module), and PORTA3 (RS signal of LCD module). Before using this function, you should install the LCD panel with screws. Adjust R28 to change the contrast of the LCD panel. #include "p18f4520.h" #include <sw_i2c.h>
2004 -2008 Sure Electronics Inc. DB-DP113_Ver2.0_Page13

PICDEM 2 PLUS SAMPLE CODE


#include <delays.h> #include <usart.h> #pragma config OSC = HS #pragma config PWRT = OFF #pragma config BOREN = OFF #pragma config WDT = OFF #pragma config MCLRE = ON #pragma config PBADEN = OFF #pragma config LVP = OFF /**************************************************************************/ /* 8-bit or 4-bit interface type * For 8-bit operation uncomment the #define BIT8 */ #define BIT8 /* When in 4-bit interface define if the data is in the upper * or lower nibble. For lower nibble, comment the #define UPPER */ /* #define UPPER */ /* DATA_PORT defines the port to which the LCD data lines are connected */ #define DATA_PORT PORTD #define TRIS_DATA_PORT TRISD /* CTRL_PORT defines the port which the control signals are connected to. Following codes are just for references. They may be amended to match your application. */ #define RW_PIN PORTAbits.RA2 /* PORT for RW */ #define TRIS_RW DDRAbits.RA2 /* TRIS for RW */ #define RS_PIN PORTAbits.RA3 /* PORT for RS */ #define TRIS_RS DDRAbits.RA3 /* TRIS for RS */ #define E_PIN PORTAbits.RA1 /* PORT for E */ #define TRIS_E DDRAbits.RA1 /* TRIS for E */ /* Display ON/OFF Control defines */ #define DON 0b00001111 /* Display on */ #define DOFF 0b00001011 /* Display off */ #define CURSOR_ON 0b00001111 /* Cursor on */ #define CURSOR_OFF 0b00001101 /* Cursor off */ #define BLINK_ON 0b00001111 /* Cursor Blink */ #define BLINK_OFF 0b00001110 /* Cursor No Blink */ /* Cursor or Display Shift defines */ #define SHIFT_CUR_LEFT 0b00010011 /* Cursor shifts to the left */ #define SHIFT_CUR_RIGHT 0b00010111 /* Cursor shifts to the right */ #define SHIFT_DISP_LEFT 0b00011011 /* Display shifts to the left */
DB-DP113_Ver2.0_Page14 2004 -2008 Sure Electronics Inc.

PICDEM 2 PLUS SAMPLE CODE


#define SHIFT_DISP_RIGHT 0b00011111 /* Display shifts to the right */ /* Function Set defines */ #define FOUR_BIT 0b00101111 #define EIGHT_BIT 0b00111111 #define LINE_5X7 0b00110011 #define LINE_5X10 0b00110111 #define LINES_5X7 0b00111011

/* 4-bit Interface */ /* 8-bit Interface */ /* 5x7 characters, single line */ /* 5x10 characters */ /* 5x7 characters, multiple line */

#define PARAM_SCLASS auto #define MEM_MODEL far /* Change this to near for small memory model */ /* OpenXLCD * Configures I/O pins for external LCD */ void OpenXLCD(PARAM_SCLASS unsigned char); /* SetCGRamAddr * Sets the character generator address */ void SetCGRamAddr(PARAM_SCLASS unsigned char); /* SetDDRamAddr * Sets the display data address */ void SetDDRamAddr(PARAM_SCLASS unsigned char); /* BusyXLCD * Returns the busy status of the LCD */ unsigned char BusyXLCD(void); /* ReadAddrXLCD * Reads the current address */ unsigned char ReadAddrXLCD(void); /* ReadDataXLCD * Reads a byte of data */ char ReadDataXLCD(void); /* WriteCmdXLCD * Writes a command to the LCD */ void WriteCmdXLCD(PARAM_SCLASS unsigned char); /* WriteDataXLCD * Writes a data byte to the LCD
2004 -2008 Sure Electronics Inc. DB-DP113_Ver2.0_Page15

PICDEM 2 PLUS SAMPLE CODE


*/ void WriteDataXLCD(PARAM_SCLASS char); /* putcXLCD * A putc is a write */ #define putcXLCD WriteDataXLCD /* putsXLCD * Writes a string of characters to the LCD */ void putsXLCD(PARAM_SCLASS char *); /* putrsXLCD * Writes a string of characters in ROM to the LCD */ void putrsXLCD(PARAM_SCLASS const MEM_MODEL rom char *); /* User defines these routines according to the oscillator frequency */ extern void DelayFor18TCY(void); extern void DelayPORXLCD(void); extern void DelayXLCD(void);

/******************************************************************** * Function Name: BusyXLCD * * Return Value: char: busy status of LCD controller * * Parameters: void * * Description: This routine reads the busy status of the * * Hitachi HD44780 LCD controller. * ********************************************************************/ unsigned char BusyXLCD(void) { RW_PIN = 1; // Set the control bits for read RS_PIN = 0; DelayFor18TCY(); E_PIN = 1; // Clock in the command DelayFor18TCY(); #ifdef BIT8 // 8-bit interface if(DATA_PORT&0x80) // Read bit 7 (busy bit) { // If high E_PIN = 0; // Reset clock line RW_PIN = 0; // Reset control line return 1; // Return TRUE } else // Bit 7 low { E_PIN = 0; // Reset clock line RW_PIN = 0; // Reset control line return 0; // Return FALSE
DB-DP113_Ver2.0_Page16 2004 -2008 Sure Electronics Inc.

PICDEM 2 PLUS SAMPLE CODE


} #else #ifdef UPPER if(DATA_PORT&0x80) #else if(DATA_PORT&0x08) #endif { E_PIN = 0; DelayFor18TCY(); E_PIN = 1; DelayFor18TCY(); E_PIN = 0; RW_PIN = 0; return 1; } else { E_PIN = 0; DelayFor18TCY(); E_PIN = 1; DelayFor18TCY(); E_PIN = 0; RW_PIN = 0; return 0; } #endif }

// 4-bit interface // Upper nibble interface // Lower nibble interface

// Reset clock line // Clock out other nibble

// Reset control line // Return TRUE // Busy bit is low // Reset clock line // Clock out other nibble

// Reset control line // Return FALSE

/******************************************************************************************* * Function Name: OpenXLCD * * Return Value: void * * Parameters: lcdtype: set the type of LCD (lines) * * Description: This routine configures the LCD. Based on * * the Hitachi HD44780 LCD controller. The * * routine will configure the I/O pins of the * * microcontroller, setup the LCD for 4- or * * 8-bit mode and clear the display. The user * * must provide three delay routines: * * DelayFor18TCY() provides a 18 Tcy delay * * DelayPORXLCD() provides at least 15ms delay * * DelayXLCD() provides at least 5ms delay * *****************************************************************************************/ void OpenXLCD(unsigned char lcdtype) { // The data bits must be from either a 8-bit port or the upper and // lower 4-bit port. These pins should be set to input #ifdef BIT8 // 8-bit mode, use whole port DATA_PORT &= 0;
2004 -2008 Sure Electronics Inc. DB-DP113_Ver2.0_Page17

PICDEM 2 PLUS SAMPLE CODE


TRIS_DATA_PORT |= 0xff; #else #ifdef UPPER DATA_PORT &= 0x0f; TRIS_DATA_PORT |= 0xf0; #else DATA_PORT &= 0xf0; TRIS_DATA_PORT |= 0x0f; #endif #endif TRIS_RW = 0; TRIS_RS = 0; TRIS_E = 0; RW_PIN = 0; RS_PIN = 0; E_PIN = 0; // 4-bit mode // Upper 4-bits of the port

// Lower 4-bits of the port

// All control signals made outputs

// R/W pin made low // Register select pin made low // Clock pin made low

// Delay for 15ms to allow for LCD Power on reset DelayPORXLCD(); // Setup interface to LCD #ifdef BIT8 TRIS_DATA_PORT &= 0; DATA_PORT &= 0; DATA_PORT |= 0b00110000; #else #ifdef UPPER TRIS_DATA_PORT &= 0x0f; DATA_PORT &= 0x0f; DATA_PORT |= 0b00100000; #else TRIS_DATA_PORT &= 0xf0; DATA_PORT &= 0xf0; DATA_PORT |= 0b00000010; #endif #endif E_PIN = 1; DelayFor18TCY(); E_PIN = 0; // Delay for at least 4.1ms DelayXLCD(); // Setup interface to LCD #ifdef BIT8 DATA_PORT &= 0; DATA_PORT |= 0b00110000; #else #ifdef UPPER DATA_PORT &= 0x0f;
DB-DP113_Ver2.0_Page18

// 8-bit mode interface // Data port output // Function set cmd(8-bit interface) // 4-bit mode interface // Upper nibble interface

// Function set cmd(4-bit interface) // Lower nibble interface

// Function set cmd(4-bit interface)

// Clock the cmd in

// 8-bit interface // Function set cmd(8-bit interface) // 4-bit interface // Upper nibble interface // Function set cmd(4-bit interface)
2004 -2008 Sure Electronics Inc.

PICDEM 2 PLUS SAMPLE CODE


DATA_PORT |= 0b00100000; #else DATA_PORT &= 0xf0; DATA_PORT |= 0b00000010; #endif #endif E_PIN = 1; DelayFor18TCY(); E_PIN = 0; // Delay for at least 100us DelayXLCD(); // Setup interface to LCD #ifdef BIT8 DATA_PORT &= 0; DATA_PORT |= 0b00110000; #else #ifdef UPPER DATA_PORT &= 0x0f; DATA_PORT |= 0b00100000; #else DATA_PORT &= 0xf0; DATA_PORT |= 0b00000010; #endif #endif E_PIN = 1; DelayFor18TCY(); E_PIN = 0; #ifdef BIT8 TRIS_DATA_PORT |= 0xff; #else #ifdef UPPER TRIS_DATA_PORT |= 0xf0; #else TRIS_DATA_PORT |= 0x0f; #endif #endif // Clock the cmd in // Lower nibble interface // Function set cmd(4-bit interface)

// 8-bit interface // Function set cmd(8-bit interface) // 4-bit interface // Upper nibble interface // Function set cmd(4-bit interface) // Lower nibble interface // Function set cmd(4-bit interface)

// Clock cmd in

// 8-bit interface // Make data port input // 4-bit interface // Upper nibble interface // Make data nibble input // Lower nibble interface // Make data nibble input

// Set data interface width, # lines, font while(BusyXLCD()); // Wait if LCD busy WriteCmdXLCD(lcdtype); // Function set cmd // Turn the display on then off while(BusyXLCD()); WriteCmdXLCD(DOFF); while(BusyXLCD()); WriteCmdXLCD(DON);
2004 -2008 Sure Electronics Inc.

// Wait if LCD busy // Display OFF/Blink OFF // Wait if LCD busy // Display ON/Blink ON
DB-DP113_Ver2.0_Page19

PICDEM 2 PLUS SAMPLE CODE


while(BusyXLCD()); // Wait if LCD busy WriteCmdXLCD(BLINK_OFF&CURSOR_OFF); ON // Display ON/Blink

// Clear display while(BusyXLCD()); WriteCmdXLCD(0x01);

// Wait if LCD busy // Clear display

// Set entry mode inc, no shift while(BusyXLCD()); // Wait if LCD busy WriteCmdXLCD(SHIFT_CUR_LEFT); // Entry Mode // Set DD Ram address to 0 while(BusyXLCD()); SetDDRamAddr(0); return; }

// Wait if LCD busy // Set Display data ram address to 0

/******************************************************************** * Function Name: putrsXLCD * Return Value: void * Parameters: buffer: pointer to string * Description: This routine writes a string of bytes to the * Hitachi HD44780 LCD controller. The user * must check to see if the LCD controller is * busy before calling this routine. The data * is written to the character generator RAM or * the display data RAM depending on what the * previous SetxxRamAddr routine was called. ********************************************************************/ /* void putrsXLCD(const rom char *buffer) { while(*buffer) // Write data to LCD up to null { while(BusyXLCD()); // Wait while LCD is busy WriteDataXLCD(*buffer); // Write character to LCD buffer++; // Increment buffer } return; } */ /******************************************************************** * Function Name: putsXLCD * Return Value: void
DB-DP113_Ver2.0_Page20 2004 -2008 Sure Electronics Inc.

PICDEM 2 PLUS SAMPLE CODE


* Parameters: buffer: pointer to string * Description: This routine writes a string of bytes to the * Hitachi HD44780 LCD controller. The user * must check to see if the LCD controller is * busy before calling this routine. The data * is written to the character generator RAM or * the display data RAM depending on what the * previous SetxxRamAddr routine was called. ********************************************************************/ void putsXLCD(char *buffer) { while(*buffer) // Write data to LCD up to null { while(BusyXLCD()); // Wait while LCD is busy WriteDataXLCD(*buffer); // Write character to LCD buffer++; // Increment buffer } return; }

/********************************************************************* * Function Name: ReadAddrXLCD * * Return Value: char: address from LCD controller * * Parameters: void * * Description: This routine reads an address byte from the * * Hitachi HD44780 LCD controller. The user * * must check to see if the LCD controller is * * busy before calling this routine. The address* * is read from the character generator RAM or * * the display data RAM depending on what the * * previous SetxxRamAddr routine was called. * *********************************************************************/ unsigned char ReadAddrXLCD(void) { char data; // Holds the data retrieved from the LCD #ifdef BIT8 RW_PIN = 1; RS_PIN = 0; DelayFor18TCY(); E_PIN = 1; DelayFor18TCY(); data = DATA_PORT; E_PIN = 0; RW_PIN = 0; #else RW_PIN = 1; RS_PIN = 0;
2004 -2008 Sure Electronics Inc.

// 8-bit interface // Set control bits for the read

// Clock data out of the LCD controller // Save the data in the register // Reset the control bits // 4-bit interface // Set control bits for the read
DB-DP113_Ver2.0_Page21

PICDEM 2 PLUS SAMPLE CODE


DelayFor18TCY(); E_PIN = 1; // Clock data out of the LCD controller DelayFor18TCY(); #ifdef UPPER // Upper nibble interface data = DATA_PORT&0xf0; // Read the nibble into the upper nibble of data #else // Lower nibble interface data = (DATA_PORT<<4)&0xf0; // Read the nibble into the upper nibble of data #endif E_PIN = 0; // Reset the clock DelayFor18TCY(); E_PIN = 1; // Clock out the lower nibble DelayFor18TCY(); #ifdef UPPER // Upper nibble interface data |= (DATA_PORT>>4)&0x0f; // Read the nibble into the lower nibble of data #else // Lower nibble interface data |= DATA_PORT&0x0f; // Read the nibble into the lower nibble of data #endif E_PIN = 0; RW_PIN = 0; // Reset the control lines #endif return (data&0x7f); // Return the address, Mask off the busy bit }

/******************************************************************** * Function Name: ReadDataXLCD * Return Value: char: data byte from LCD controller * * Parameters: void * * Description: This routine reads a data byte from the * * Hitachi HD44780 LCD controller. The user * * must check to see if the LCD controller is * * busy before calling this routine. The data * * is read from the character generator RAM or * * the display data RAM depending on what the * * previous SetxxRamAddr routine was called. * ********************************************************************/ char ReadDataXLCD(void) { char data; #ifdef BIT8 RS_PIN = 1; RW_PIN = 1; DelayFor18TCY(); E_PIN = 1; DelayFor18TCY();
DB-DP113_Ver2.0_Page22

// 8-bit interface // Set the control bits

// Clock the data out of the LCD


2004 -2008 Sure Electronics Inc.

PICDEM 2 PLUS SAMPLE CODE


data = DATA_PORT; E_PIN = 0; RS_PIN = 0; RW_PIN = 0; #else // Read the data // Reset the control bits

// 4-bit interface RW_PIN = 1; RS_PIN = 1; DelayFor18TCY(); E_PIN = 1; // Clock the data out of the LCD DelayFor18TCY(); #ifdef UPPER // Upper nibble interface data = DATA_PORT&0xf0; // Read the upper nibble of data #else // Lower nibble interface data = (DATA_PORT<<4)&0xf0; // read the upper nibble of data #endif E_PIN = 0; // Reset the clock line DelayFor18TCY(); E_PIN = 1; // Clock the next nibble out of the LCD DelayFor18TCY(); #ifdef UPPER // Upper nibble interface data |= (DATA_PORT>>4)&0x0f; // Read the lower nibble of data #else // Lower nibble interface data |= DATA_PORT&0x0f; // Read the lower nibble of data #endif E_PIN = 0; RS_PIN = 0; // Reset the control bits RW_PIN = 0; #endif return(data); // Return the data byte }

/******************************************************************** * Function Name: SetCGRamAddr * * Return Value: void * * Parameters: CGaddr: character generator ram address * * Description: This routine sets the character generator * * address of the Hitachi HD44780 LCD * * controller. The user must check to see if * * the LCD controller is busy before calling * * this routine. * ********************************************************************/ void SetCGRamAddr(unsigned char CGaddr) { #ifdef BIT8 // 8-bit interface TRIS_DATA_PORT = 0; // Make data port ouput DATA_PORT = CGaddr | 0b01000000; // Write cmd and address to port RW_PIN = 0; // Set control signals RS_PIN = 0;
2004 -2008 Sure Electronics Inc. DB-DP113_Ver2.0_Page23

PICDEM 2 PLUS SAMPLE CODE


DelayFor18TCY(); E_PIN = 1; DelayFor18TCY(); E_PIN = 0; DelayFor18TCY(); TRIS_DATA_PORT = 0xff; // Clock cmd and address in

// Make data port inputs #else // 4-bit interface #ifdef UPPER // Upper nibble interface TRIS_DATA_PORT &= 0x0f; // Make nibble input DATA_PORT &= 0x0f; // and write upper nibble DATA_PORT |= ((CGaddr | 0b01000000) & 0xf0); #else // Lower nibble interface TRIS_DATA_PORT &= 0xf0; // Make nibble input DATA_PORT &= 0xf0; // and write upper nibble DATA_PORT |= (((CGaddr |0b01000000)>>4) & 0x0f); #endif RW_PIN = 0; // Set control signals RS_PIN = 0; DelayFor18TCY(); E_PIN = 1; // Clock cmd and address in DelayFor18TCY(); E_PIN = 0; #ifdef UPPER // Upper nibble interface DATA_PORT &= 0x0f; // Write lower nibble DATA_PORT |= ((CGaddr<<4)&0xf0); #else // Lower nibble interface DATA_PORT &= 0xf0; // Write lower nibble DATA_PORT |= (CGaddr&0x0f); #endif DelayFor18TCY(); E_PIN = 1; // Clock cmd and address in DelayFor18TCY(); E_PIN = 0; #ifdef UPPER // Upper nibble interface TRIS_DATA_PORT |= 0xf0; // Make inputs #else // Lower nibble interface TRIS_DATA_PORT |= 0x0f; // Make inputs #endif #endif return; }

/******************************************************************** * Function Name: SetDDRamAddr * Return Value: void * * Parameters: CGaddr: display data address * * Description: This routine sets the display data address * * of the Hitachi HD44780 LCD controller. The * * user must check to see if the LCD controller*
DB-DP113_Ver2.0_Page24

2004 -2008 Sure Electronics Inc.

PICDEM 2 PLUS SAMPLE CODE


* is busy before calling this routine. * ********************************************************************/ void SetDDRamAddr(unsigned char DDaddr) { #ifdef BIT8 // 8-bit interface TRIS_DATA_PORT = 0; // Make port output DATA_PORT = DDaddr | 0b10000000; // Write cmd and address to port RW_PIN = 0; // Set the control bits RS_PIN = 0; DelayFor18TCY(); E_PIN = 1; // Clock the cmd and address in DelayFor18TCY(); E_PIN = 0; DelayFor18TCY(); TRIS_DATA_PORT = 0xff; // Make port input #else // 4-bit interface #ifdef UPPER // Upper nibble interface TRIS_DATA_PORT &= 0x0f; // Make port output DATA_PORT &= 0x0f; // and write upper nibble DATA_PORT |= ((DDaddr | 0b10000000) & 0xf0); #else // Lower nibble interface TRIS_DATA_PORT &= 0xf0; // Make port output DATA_PORT &= 0xf0; // and write upper nibble DATA_PORT |= (((DDaddr | 0b10000000)>>4) & 0x0f); #endif RW_PIN = 0; // Set control bits RS_PIN = 0; DelayFor18TCY(); E_PIN = 1; // Clock the cmd and address in DelayFor18TCY(); E_PIN = 0; #ifdef UPPER // Upper nibble interface DATA_PORT &= 0x0f; // Write lower nibble DATA_PORT |= ((DDaddr<<4)&0xf0); #else // Lower nibble interface DATA_PORT &= 0xf0; // Write lower nibble DATA_PORT |= (DDaddr&0x0f); #endif DelayFor18TCY(); E_PIN = 1; // Clock the cmd and address in DelayFor18TCY(); E_PIN = 0; #ifdef UPPER // Upper nibble interface TRIS_DATA_PORT |= 0xf0; // Make port input #else // Lower nibble interface TRIS_DATA_PORT |= 0x0f; // Make port input #endif #endif return;
2004 -2008 Sure Electronics Inc. DB-DP113_Ver2.0_Page25

PICDEM 2 PLUS SAMPLE CODE


}

/******************************************************************** * Function Name: WriteCmdXLCD * * Return Value: void * * Parameters: cmd: command to send to LCD * * Description: This routine writes a command to the Hitachi* * HD44780 LCD controller. The user must check * * to see if the LCD controller is busy before * * calling this routine. * ********************************************************************/ void WriteCmdXLCD(unsigned char cmd) { #ifdef BIT8 // 8-bit interface TRIS_DATA_PORT &= 0; // Data port output DATA_PORT &= 0; DATA_PORT |= cmd; // Write command to data port RW_PIN = 0; // Set the control signals RS_PIN = 0; // for sending a command DelayFor18TCY(); E_PIN = 1; // Clock the command in DelayFor18TCY(); E_PIN = 0; DelayFor18TCY(); TRIS_DATA_PORT |= 0xff; // Data port input #else // 4-bit interface #ifdef UPPER // Upper nibble interface TRIS_DATA_PORT &= 0x0f; DATA_PORT &= 0x0f; DATA_PORT |= cmd&0xf0; #else // Lower nibble interface TRIS_DATA_PORT &= 0xf0; DATA_PORT &= 0xf0; DATA_PORT |= (cmd>>4)&0x0f; #endif RW_PIN = 0; // Set control signals for command RS_PIN = 0; DelayFor18TCY(); E_PIN = 1; // Clock command in DelayFor18TCY(); E_PIN = 0; #ifdef UPPER // Upper nibble interface DATA_PORT &= 0x0f; DATA_PORT |= (cmd<<4)&0xf0; #else // Lower nibble interface DATA_PORT &= 0xf0; DATA_PORT |= cmd&0x0f; #endif DelayFor18TCY();
DB-DP113_Ver2.0_Page26 2004 -2008 Sure Electronics Inc.

PICDEM 2 PLUS SAMPLE CODE


E_PIN = 1; DelayFor18TCY(); E_PIN = 0; #ifdef UPPER TRIS_DATA_PORT |= 0xf0; #else TRIS_DATA_PORT |= 0x0f; #endif #endif return; } // Clock command in

// Make data nibble input

/******************************************************************** * Function Name: WriteDataXLCD * * Return Value: void * * Parameters: data: data byte to be written to LCD * * Description: This routine writes a data byte to the * * Hitachi HD44780 LCD controller. The user * * must check to see if the LCD controller is * * busy before calling this routine. The data * * is written to the character generator RAM or* * the display data RAM depending on what the * * previous SetxxRamAddr routine was called. * ********************************************************************/ void WriteDataXLCD(char data) { #ifdef BIT8 // 8-bit interface TRIS_DATA_PORT = 0; // Make port output DATA_PORT = data; // Write data to port RS_PIN = 1; // Set control bits RW_PIN = 0; DelayFor18TCY(); E_PIN = 1; // Clock data into LCD DelayFor18TCY(); E_PIN = 0; RS_PIN = 0; // Reset control bits TRIS_DATA_PORT = 0xff; // Make port input #else // 4-bit interface #ifdef UPPER // Upper nibble interface TRIS_DATA_PORT &= 0x0f; DATA_PORT &= 0x0f; DATA_PORT |= data&0xf0; #else // Lower nibble interface TRIS_DATA_PORT &= 0xf0; DATA_PORT &= 0xf0; DATA_PORT |= ((data>>4)&0x0f); #endif RS_PIN = 1; // Set control bits
2004 -2008 Sure Electronics Inc. DB-DP113_Ver2.0_Page27

PICDEM 2 PLUS SAMPLE CODE


RW_PIN = 0; DelayFor18TCY(); E_PIN = 1; DelayFor18TCY(); E_PIN = 0; #ifdef UPPER DATA_PORT &= 0x0f; DATA_PORT |= ((data<<4)&0xf0); #else DATA_PORT &= 0xf0; DATA_PORT |= (data&0x0f); #endif DelayFor18TCY(); E_PIN = 1; DelayFor18TCY(); E_PIN = 0; #ifdef UPPER TRIS_DATA_PORT |= 0xf0; #else TRIS_DATA_PORT |= 0x0f; #endif #endif return; } void DelayFor18TCY( void ) { Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); } void DelayPORXLCD (void) { Delay1KTCYx(75); // Delay of 15ms // Cycles = (TimeDelay * Fosc) / 4 // Cycles = (15ms * 20MHz) / 4 // Cycles = 75,000 return; } void DelayXLCD (void) {
DB-DP113_Ver2.0_Page28 2004 -2008 Sure Electronics Inc.

// Clock nibble into LCD

// Upper nibble interface

// Lower nibble interface

// Clock nibble into LCD

// Upper nibble interface // Lower nibble interface

PICDEM 2 PLUS SAMPLE CODE


Delay1KTCYx(25); // Delay of 5ms // Cycles = (TimeDelay * Fosc) / 4 // Cycles = (5ms * 20MHz) / 4 // Cycles = 25,000 return; } // Initializtion void init(void) { CMCON=0b00000111; TRISA=0b00010000; TRISB=0b00000001; TRISC=0b00000000; TRISD=0b00000000; TRISE=0b00001000; ADCON1=0b00001111; }

// Close Comparator

// Configure Digital Channel

// Main Programmer void main( void ) { unsigned char i; char display_name[]="Sure Electronics"; char display_Ver[]="Ver 2.1"; init(); // Configure external LCD OpenXLCD( EIGHT_BIT&LINES_5X7 ); // Write to LCD while(BusyXLCD()); putsXLCD(display_name); // Write to LCD while(BusyXLCD()); SetDDRamAddr(0x40); putsXLCD(display_Ver); while(1){}; }

// Wait if LCD busy

// Wait if LCD busy

2004 -2008 Sure Electronics Inc.

DB-DP113_Ver2.0_Page29

PICDEM 2 PLUS SAMPLE CODE Sample 8. LED Mode Code

This demonstration shows how to display message on the 7segment LEDs through GPIOs of PIC18F4520 microcontroller. The 8 bits data output of PORTD is connected to a-h of 7segment LEDs, and PORTB4-7 is connected to base of ULN2003, then drive 4 cathode of those LEDs. Remove the LCD panel on the LED segments. Send data from PORTD and enable one of the cathodes. The LED segments will display a digit. If switch-on and switch-off of the four cathodes in turn quickly enough, it will display 4 digits just like simultaneously. That looks seamlessly. #include <p18f4520.h> #include <sw_i2c.h> #include <usart.h> #include <delays.h> #include <timers.h> #pragma config OSC = HSPLL #pragma config PWRT = OFF #pragma config BOREN = OFF #pragma config WDT = OFF #pragma config MCLRE = ON #pragma config PBADEN = OFF #pragma config LVP = OFF #define leddata_port PORTD #define ledcom_port0 PORTBbits.RB4 #define ledcom_port1 PORTBbits.RB5 #define ledcom_port2 PORTBbits.RB6 #define ledcom_port3 PORTBbits.RB7 #define Func_key PORTBbits.RB0 #define change_key PORTAbits.RA4 unsigned char i2c_var; unsigned int cvalue,fvalue; char cent_buf[6],fahr_buf[6]; // High-Speed Crystal/Resonator // with PLL enabled

// Function key // Switch key

// Memory Centigrade and Fahrenheit value // Centigrade and Fahrenheit value array

unsigned char reset_key_pressed,last_reset_key_pressed; unsigned char Func_key_pressed,last_Func_key_pressed; unsigned char change_key_pressed,last_change_key_pressed; unsigned char LED_thousand,LED_hundred,LED_ten,LED_one; unsigned char value_thousand,value_hundred,value_ten,value_one; unsigned char temperature_flag; unsigned char cnt_fuckey; unsigned char key_flag=0; char pointer=0;
DB-DP113_Ver2.0_Page30 2004 -2008 Sure Electronics Inc.

PICDEM 2 PLUS SAMPLE CODE

// a total of cathodic const unsigned char Digital_TAB[]= {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f, 0x6d,0x1c,0x50,0x79,0x86,0x06}; // 0123456789 SurE 1.1

void timer_isr (void); void init(void); char Funckey(void); void changekey(void); void LED_init(void); void PWM(unsigned char i); void LED_data(unsigned int data); void LED_display(unsigned char i); void sendUSART(char *sendbuf);

// Interrupt // Initialize Control Microchip // Get State of Function Switch // Get State of Change Display Switch // Set LED Initial Display // PWM Mode // Data for LED Display // LED Display // Send Data to PC

/**************************************************************************/ void init(void) { CMCON=0b00000111; // Close Comparator TRISA=0b00010000; // RA4 is for Switch SW1 Input TRISB=0b00000001; // RB0 is for Switch SW2 Input TRISC=0b11000000; TRISD=0b00000000; TRISE=0b00001000; // RE3 is for Switch SW3 Input ADCON1=0b00001111; // Digital Channel Allocation SPBRG=155; BAUDCONbits.BRG16=0; TXSTAbits.BRGH=0; TXSTAbits.SYNC=0; RCSTAbits.SPEN=1; TXSTAbits.TX9=0; TXSTAbits.TXEN=1; // Baud Rate 4800bps // Choose 8-bit Baud Rate Generator // High Baud Rate // Asynchronous Mode // Enable Serial // 8-bit Transmission // Enable Transmission

} /**************************************************************************/ void PWM(unsigned char i) { CCP1CON=0b00001100; T2CONbits.TMR2ON = 0; T2CONbits.T2OUTPS3 = 1; T2CONbits.T2OUTPS2 = 1; T2CONbits.T2OUTPS1 = 1; T2CONbits.T2OUTPS0 = 1; T2CONbits.T2CKPS1 = 1; T2CONbits.T2CKPS1 = 1; PR2 = 255; TRISCbits.TRISC2=0;
2004 -2008 Sure Electronics Inc. DB-DP113_Ver2.0_Page31

PICDEM 2 PLUS SAMPLE CODE


T2CONbits.TMR2ON = 1; CCPR1L = 25*i; } /**************************************************************************/ char Funckey(void) { unsigned char temp; Func_key_pressed=Func_key; if((Func_key_pressed==0)&(last_Func_key_pressed==1)) { Delay100TCYx(1); //Delay to avoid unexpected buffeting if(Func_key_pressed==0)cnt_fuckey++; } last_Func_key_pressed=Func_key_pressed; temp=cnt_fuckey%4; return temp; } void changekey(void) { change_key_pressed=change_key; if((change_key_pressed==0)&(last_change_key_pressed==1)) { Delay100TCYx(1); //Delay to avoid unexpected buffeting if(change_key_pressed==0)return; } last_change_key_pressed=change_key_pressed; } /**************************************************************************/ // Write data void byte_write(unsigned char adr,unsigned char data) { SWStartI2C(); i2c_var = SWPutcI2C(0xA0); // Control Byte SWAckI2C(); i2c_var = SWPutcI2C(adr); // Word Address SWAckI2C(); i2c_var = SWPutcI2C(data); // Write Data SWAckI2C(); SWStopI2C(); } // Read data void byte_read(unsigned char adr) { SWStartI2C(); i2c_var = SWPutcI2C( 0xA0 ); SWAckI2C();
DB-DP113_Ver2.0_Page32

// Control Byte
2004 -2008 Sure Electronics Inc.

PICDEM 2 PLUS SAMPLE CODE


i2c_var = SWPutcI2C(adr); SWAckI2C(); SWRestartI2C(); i2c_var = SWPutcI2C( 0xA1 ); SWAckI2C(); i2c_var = SWGetcI2C();//data SWStopI2C(); } // Write string void page_write(unsigned char adr,unsigned char wdata[]) { SWStartI2C(); i2c_var = SWPutcI2C(0xA0); // Control Byte SWAckI2C(); i2c_var = SWPutcI2C(adr); // Word Address SWAckI2C(); i2c_var = SWPutsI2C(wdata); // Write Data SWStopI2C(); } // Read string void sequential_read(unsigned char adr,unsigned char rdata[],unsigned char len) { SWStartI2C(); i2c_var = SWPutcI2C( 0xA0 ); // Control Byte SWAckI2C(); i2c_var = SWPutcI2C(adr); // Word Address SWAckI2C(); SWRestartI2C(); i2c_var = SWPutcI2C( 0xA1 ); // Control Byte SWAckI2C(); i2c_var = SWGetsI2C(rdata,len); // Get Data SWStopI2C(); } // Inquiries confirmed void ack_poll( void ) { SWStartI2C(); i2c_var = SWPutcI2C( 0xA0 ); while( SWAckI2C() ) { SWRestartI2C(); i2c_var = SWPutcI2C(0xA0); } SWStopI2C(); }
2004 -2008 Sure Electronics Inc.

// Word Address

// Control Byte

// Control Byte

// Write Data

DB-DP113_Ver2.0_Page33

PICDEM 2 PLUS SAMPLE CODE


/**************************************************************************/ void LM75_init(void) //Temperature Sensor Initializtion { SWStartI2C(); i2c_var = SWPutcI2C(0x90); //Control Byte SWAckI2C(); i2c_var = SWPutcI2C(0x01); //Configure Register SWAckI2C(); i2c_var = SWPutcI2C(0x18); //Configure Byte SWAckI2C(); SWStopI2C(); } void LM75_temperature(void) { unsigned char tptr[2]; unsigned int temp_H,temp_L; SWStartI2C(); i2c_var = SWPutcI2C(0x90); SWAckI2C(); i2c_var = SWPutcI2C(0x00); SWAckI2C(); SWRestartI2C(); i2c_var = SWPutcI2C(0x91); SWAckI2C(); i2c_var = SWGetsI2C(tptr, 2); SWStopI2C(); temp_H=tptr[0]; temp_L=tptr[1]; //Compute Centigrade cvalue=(temp_H<<8)|temp_L; cent_buf[0]=' '; if(cvalue&0x80==1) { cvalue=~cvalue+1; cent_buf[0]='-'; } cvalue=cvalue>>5; cvalue=cvalue * 1.25; cent_buf[1]=cvalue/100+48; cent_buf[2]=(cvalue/10)%10+48; cent_buf[3]='.'; cent_buf[4]=cvalue%10+48; cent_buf[5]='\0'; //compute Fahrenheit fvalue=((cvalue*9)/5)+32; fahr_buf[0]=' ';
DB-DP113_Ver2.0_Page34 2004 -2008 Sure Electronics Inc.

//control byte //Data Address

//Control Byte //Read Temperature

//High bits //Low bits

//Calculate Base Complement

PICDEM 2 PLUS SAMPLE CODE


if(fvalue&0x80==1) { fvalue=~fvalue+1; fahr_buf[0]='-'; } fahr_buf[1]=fvalue/100+48; fahr_buf[2]=(fvalue/10)%10+48; fahr_buf[3]='.'; fahr_buf[4]=fvalue%10+48; fahr_buf[5]='\0'; } /**************************************************************************/ void LED_init(void) { unsigned char i,j; unsigned char temp; while(1) { i++; j=i%4; LED_display(j); //Display Characters if(i==1) { temp++; if(temp==2) //Display "Sure" when Power on or after Resetting { value_thousand=0x6d; value_hundred=0x1c; value_ten=0x50; value_one=0x79; } if(temp==120) //Then Display "1.1" at some Intervals { value_thousand=0x00; value_hundred=0x00; value_ten=0x86; value_one=0x06; } } if(temp==255)break; } } void LED_display(unsigned char i) { ledcom_port0=0; ledcom_port1=0;
2004 -2008 Sure Electronics Inc. DB-DP113_Ver2.0_Page35

//Calculate Base Complement

PICDEM 2 PLUS SAMPLE CODE


ledcom_port2=0; ledcom_port3=0; switch(i) { case(0): { ledcom_port3=1; leddata_port=value_thousand; }break; case(1): { ledcom_port2=1; leddata_port=value_hundred; }break; case(2): { ledcom_port1=1; leddata_port=value_ten; }break; case(3): { ledcom_port0=1; leddata_port=value_one; }break; } } void LED_data(unsigned int data) { unsigned int i; LED_thousand=0; LED_hundred=0; LED_ten=0; LED_one=0; for(i=0;i<data;i++) //Data is divided into four digits { LED_one++; if(LED_one>=10){LED_one=0;LED_ten++;} if(LED_ten>=10){LED_ten=0;LED_hundred++;} if(LED_hundred>=10){LED_hundred=0;LED_thousand++;} } for(i=0;i<10;i++) // Compute which digit to display on corresponding position { if(i==LED_thousand){value_thousand=Digital_TAB[i];} if(i==LED_hundred){value_hundred=Digital_TAB[i];} if(i==LED_ten){value_ten=Digital_TAB[i];} if(i==LED_one){value_one=Digital_TAB[i];} }
DB-DP113_Ver2.0_Page36 2004 -2008 Sure Electronics Inc.

//Display Top Digit

//Display Second-order Digit

//Display Third-order Digit

//Display Least Significant Digit

PICDEM 2 PLUS SAMPLE CODE


if(temperature_flag==1) { value_ten=value_ten|0x80; temperature_flag=0; } } /**************************************************************************/ #pragma code low_vector=0x18 void low_interrupt (void) { _asm GOTO timer_isr _endasm } #pragma code #pragma interruptlow timer_isr void timer_isr (void) { unsigned char i,j,k; unsigned char temp,send; char Cent[]="Centigrade"; char Fahr[]="Fahrenheit"; char Cont[]="Contrast"; char Current[]="Current Temperatrue"; TMR0H=0X80; TMR0L=0X00; i++; j=i%4; LED_display(j); if(i==0) { LM75_temperature(); }

// Temperature value should have // a decimal point

//Not more than 4-digit data //Display Characters

//Read Temperature Value

//Display one piece of information at a time if(key_flag==0) { k=Funckey(); switch(k) { case(1): //Display "Sure" { value_thousand=0x6d; value_hundred=0x1c; value_ten=0x50; value_one=0x79;
2004 -2008 Sure Electronics Inc. DB-DP113_Ver2.0_Page37

PICDEM 2 PLUS SAMPLE CODE


}break; case(2): //Display "1.1" { value_thousand=0x00; value_hundred=0x00; value_ten=0x86; value_one=0x06; }break; case(3): //Display Centigrade Value { temperature_flag=1; LED_data(cvalue); }break; case(0): //Display Fahrenheit Value { temperature_flag=1; LED_data(fvalue); }break; } if(change_key==0)key_flag=1; } if(key_flag==1) { if(i%8==0) { temp++; if(temp%64==15) { value_thousand=0x6d; value_hundred=0x1c; value_ten=0x50; value_one=0x79; } if(temp%64==31) { value_thousand=0x00; value_hundred=0x00; value_ten=0x86; value_one=0x06; } if(temp%64==47) { temperature_flag=1; LED_data(cvalue); } if(temp%64==0) { temperature_flag=1; LED_data(fvalue);
DB-DP113_Ver2.0_Page38

//Display information in turn

//Display "Sure"

//Display "1.1"

//Display Centigrade Value

//Display Fahrenheit Value

2004 -2008 Sure Electronics Inc.

PICDEM 2 PLUS SAMPLE CODE


} } if(Func_key==0)key_flag=0; } if(i%8==0) { send++; if((send>0)&(send<24)) { PWM(4); sendUSART(Current); } if(send==24) { PWM(0); pointer=0; TXREG=13; TXREG=10; } if((send>28)&(send<36)) { sendUSART(cent_buf); } if(send==36) { pointer=0; } if((send>36)&(send<52)) { sendUSART(Cent); } if(send==52) { pointer=0; TXREG=13; TXREG=10; } if((send>56)&(send<64)) { sendUSART(Fahr); } if(send==64) { pointer=0; } if((send>64)&(send<80)) { sendUSART(Fahr);
2004 -2008 Sure Electronics Inc.

//Buzzer Beep //Send "Current Temperatrue" to PC

//Enter //Newline

//Send Centigrade Temperature Value

//Send Fahrenheit Temperature Value

DB-DP113_Ver2.0_Page39

PICDEM 2 PLUS SAMPLE CODE


} if(send==80) { pointer=0; TXREG=13; TXREG=10; } } INTCONbits.TMR0IF = 0; } void sendUSART(char *sendbuf) { if(*(sendbuf+pointer)!=0) { if(BusyUSART()==0) { TXREG=*(sendbuf+pointer); pointer++; return; } else { return; } } } /**************************************************************************/ void main (void) { init(); //Initialize Control Microchip LM75_init(); //Temperatrue Sensor Initializtion LED_init(); //Open LED to display "SurE" and "1.1" OpenTimer0 (TIMER_INT_ON & T0_SOURCE_INT & T0_16BIT); INTCONbits.GIE = 1; while(1){}; }

Sample 9. Display Information on the LCD Module.

This Sample shows how to display information on the HD44780 compatible LCD Module. The LCD module is connected to PORTD (as data port), PORTA1 (E signal of LCD module), PORTA2 (RW signal of LCD module), and PORTA3 (RS signal of LCD module), PORTC1 (CCP2 as PWM controlled DC voltage generator).
DB-DP113_Ver2.0_Page40 2004 -2008 Sure Electronics Inc.

PICDEM 2 PLUS SAMPLE CODE


Before using this function, you should install the LCD panel with screws first. Adjust the contrast of the LCD panel via PORTC1. And you could adjust the contrast with potentiometer R28 too. #include <p18f4520.h> #include <sw_i2c.h> #include <usart.h> #include <delays.h> #include <timers.h> #pragma config OSC = HS #pragma config PWRT = OFF #pragma config BOREN = OFF #pragma config WDT = OFF #pragma config MCLRE = ON #pragma config PBADEN = OFF #pragma config LVP = OFF #define Func_key PORTBbits.RB0 #define change_key PORTAbits.RA4 // Function key // Change key

unsigned char reset_key_pressed,last_reset_key_pressed; unsigned char Func_key_pressed,last_Func_key_pressed; unsigned char change_key_pressed,last_change_key_pressed; unsigned char cnt_fuckey; unsigned char cnt_chgkey; unsigned char PWM_data; char PWMbuf[6]; unsigned char key_flag=0; char pointer=0;

unsigned char i2c_var; unsigned int cvalue,fvalue; char cent_buf[6],fahr_buf[6]; char LCD_name[]="Sure Electronics"; char LCD_Ver[]="Ver 2.1"; void timer_isr (void); void init(void); char Funckey(void); char changekey(void); void PWM(unsigned char i); void LCD_init(void); void LCD_display(char *i); void delayinit(void); void sendUSART(char *sendbuf);
2004 -2008 Sure Electronics Inc.

// Memory Centigrade and Fahrenheit Value // Centigrade and Fahrenheit Value Array

// Timer0 Interrupt Service Routine // Initialize Control Microchip // Get State of Function Switch // Get State of Change Function Switch // PWM Mode // Set LCD Initial Display // LCD display

DB-DP113_Ver2.0_Page41

PICDEM 2 PLUS SAMPLE CODE


/**************************************************************************/ void init(void) { CMCON=0b00000111; // Close Comparator TRISA=0b00010000; TRISB=0b00000001; TRISC=0b11000000; TRISD=0b00000000; TRISE=0b00001000; ADCON1=0b00001111; // Digital Channel Allocation SPBRG=38; BAUDCONbits.BRG16=0; TXSTAbits.BRGH=0; TXSTAbits.SYNC=0; RCSTAbits.SPEN=1; TXSTAbits.TX9=0; TXSTAbits.TXEN=1; CCP1CON=0b00001100; CCP2CON=0b00001100; T2CONbits.TMR2ON = 0; T2CONbits.T2OUTPS3 = 1; T2CONbits.T2OUTPS2 = 1; T2CONbits.T2OUTPS1 = 1; T2CONbits.T2OUTPS0 = 1; T2CONbits.T2CKPS1 = 1; T2CONbits.T2CKPS1 = 1; PR2 = 255; TRISCbits.TRISC2=0; TRISCbits.TRISC1=0; T2CONbits.TMR2ON = 1; CCPR1L = 100; CCPR2L = 0; } /**************************************************************************/ void PWM(unsigned char i) { CCP1CON=0b00001100; T2CONbits.TMR2ON = 0; T2CONbits.T2OUTPS3 = 1; T2CONbits.T2OUTPS2 = 1; T2CONbits.T2OUTPS1 = 1; T2CONbits.T2OUTPS0 = 1; T2CONbits.T2CKPS1 = 1; T2CONbits.T2CKPS1 = 1; PR2 = 255; TRISCbits.TRISC2=0; T2CONbits.TMR2ON = 1;
DB-DP113_Ver2.0_Page42 2004 -2008 Sure Electronics Inc.

// Baud Rate 4800bps // Choose 8-bit Baud Rate Generator // High Baud Rate // Asynchronous Mode // Enable Serial // 8-bit Transmission // Enable Transmission // Set PWM Mode

PICDEM 2 PLUS SAMPLE CODE


CCPR1L = 25*i; } /**************************************************************************/ char Funckey(void) { unsigned char temp; Func_key_pressed=Func_key; if((Func_key_pressed==0)&(last_Func_key_pressed==1)) { Delay100TCYx(1); //Delay to avoid buffeting if(Func_key_pressed==0)cnt_fuckey++; } last_Func_key_pressed=Func_key_pressed; temp=cnt_fuckey%3; return temp; } char changekey(void) { unsigned char temp; change_key_pressed=change_key; if((change_key_pressed==0)&(last_change_key_pressed==1)) { Delay100TCYx(1); //Delay to avoid buffeting if(change_key_pressed==0)cnt_chgkey++; } last_change_key_pressed=change_key_pressed; temp=cnt_chgkey%11; return temp; } /**************************************************************************/ // Write data void byte_write(unsigned char adr,unsigned char data) { SWStartI2C(); i2c_var = SWPutcI2C(0xA0); // Control byte SWAckI2C(); i2c_var = SWPutcI2C(adr); // Word address SWAckI2C(); i2c_var = SWPutcI2C(data); // Data SWAckI2C(); SWStopI2C(); } // Read data
2004 -2008 Sure Electronics Inc. DB-DP113_Ver2.0_Page43

PICDEM 2 PLUS SAMPLE CODE


void byte_read(unsigned char adr) { SWStartI2C(); i2c_var = SWPutcI2C( 0xA0 ); SWAckI2C(); i2c_var = SWPutcI2C(adr); SWAckI2C(); SWRestartI2C(); i2c_var = SWPutcI2C( 0xA1 ); SWAckI2C(); i2c_var = SWGetcI2C();//data SWStopI2C(); }

// Control byte // Word address

// Control byte

// Write string void page_write(unsigned char adr,unsigned char wdata[]) { SWStartI2C(); i2c_var = SWPutcI2C(0xA0); // Control byte SWAckI2C(); i2c_var = SWPutcI2C(adr); // Word address SWAckI2C(); i2c_var = SWPutsI2C(wdata); // Data SWStopI2C(); } // Read string void sequential_read(unsigned char adr,unsigned char rdata[],unsigned char len) { SWStartI2C(); i2c_var = SWPutcI2C( 0xA0 ); // Control byte SWAckI2C(); i2c_var = SWPutcI2C(adr); // Word address SWAckI2C(); SWRestartI2C(); i2c_var = SWPutcI2C( 0xA1 ); // Control byte SWAckI2C(); i2c_var = SWGetsI2C(rdata,len); // Data SWStopI2C(); } // Inquiries confirmed void ack_poll( void ) { SWStartI2C(); i2c_var = SWPutcI2C( 0xA0 ); while( SWAckI2C() ) { SWRestartI2C(); i2c_var = SWPutcI2C(0xA0);
DB-DP113_Ver2.0_Page44

// Control byte

// Data
2004 -2008 Sure Electronics Inc.

PICDEM 2 PLUS SAMPLE CODE


} SWStopI2C(); } /**************************************************************************/ void LM75_init(void) // Temperature Sensor Initializtion { SWStartI2C(); i2c_var = SWPutcI2C(0x90); // Control byte SWAckI2C(); i2c_var = SWPutcI2C(0x01); // Configure register SWAckI2C(); i2c_var = SWPutcI2C(0x18); // Configure byte SWAckI2C(); SWStopI2C(); } void LM75_temperature(void) { unsigned char tptr[2]; unsigned int temp_H,temp_L; SWStartI2C(); i2c_var = SWPutcI2C(0x90); SWAckI2C(); i2c_var = SWPutcI2C(0x00); SWAckI2C(); SWRestartI2C(); i2c_var = SWPutcI2C(0x91); SWAckI2C(); i2c_var = SWGetsI2C(tptr, 2); SWStopI2C(); temp_H=tptr[0]; temp_L=tptr[1];

// Control byte // Data Address

// Control byte // Read Temperature

// High bits // Low bits

// Compute Centigrade cvalue=(temp_H<<8)|temp_L; cent_buf[0]=' '; if(cvalue&0x80==1) { cvalue=~cvalue+1; // Calculate Base Complement cent_buf[0]='-'; } cvalue=cvalue>>5; cvalue=cvalue * 1.25; cent_buf[1]=cvalue/100+48; cent_buf[2]=(cvalue/10)%10+48; cent_buf[3]='.'; cent_buf[4]=cvalue%10+48;
2004 -2008 Sure Electronics Inc. DB-DP113_Ver2.0_Page45

PICDEM 2 PLUS SAMPLE CODE


cent_buf[5]='\0'; // Compute Fahrenheit fvalue=((cvalue*9)/5)+32; fahr_buf[0]=' '; if(fvalue&0x80==1) { fvalue=~fvalue+1; // Calculate Base Complement fahr_buf[0]='-'; } fahr_buf[1]=fvalue/100+48; fahr_buf[2]=(fvalue/10)%10+48; fahr_buf[3]='.'; fahr_buf[4]=fvalue%10+48; fahr_buf[5]='\0'; } /**************************************************************************/ /* 8-bit or 4-bit interface type * For 8-bit operation uncomment the #define BIT8 */ #define BIT8 /* When in 4-bit interface define if the data is in the upper or lower nibble. For lower nibble, comment the #define UPPER */ /* #define UPPER */ /* DATA_PORT defines the port which the LCD data lines are connected to */ #define DATA_PORT PORTD #define TRIS_DATA_PORT TRISD /* CTRL_PORT defines the port where the control lines are connected. * These are just samples, change to match your application. */ #define RW_PIN PORTAbits.RA2 /* PORT for RW */ #define TRIS_RW DDRAbits.RA2 /* TRIS for RW */ #define RS_PIN PORTAbits.RA3 /* PORT for RS */ #define TRIS_RS DDRAbits.RA3 /* TRIS for RS */ #define E_PIN PORTAbits.RA1 /* PORT for E */ #define TRIS_E DDRAbits.RA1 /* TRIS for E */ /* Display ON/OFF Control defines */ #define DON 0b00001111 #define DOFF 0b00001011 #define CURSOR_ON 0b00001111 #define CURSOR_OFF 0b00001101 #define BLINK_ON 0b00001111 #define BLINK_OFF 0b00001110
DB-DP113_Ver2.0_Page46

/* Display on */ /* Display off */ /* Cursor on */ /* Cursor off */ /* Cursor Blink */ /* Cursor No Blink */
2004 -2008 Sure Electronics Inc.

PICDEM 2 PLUS SAMPLE CODE


/* Cursor or Display Shift defines */ #define SHIFT_CUR_LEFT 0b00010011 #define SHIFT_CUR_RIGHT 0b00010111 #define SHIFT_DISP_LEFT 0b00011011 #define SHIFT_DISP_RIGHT 0b00011111 /* Function Set defines */ #define FOUR_BIT 0b00101111 #define EIGHT_BIT 0b00111111 #define LINE_5X7 0b00110011 #define LINE_5X10 0b00110111 #define LINES_5X7 0b00111011

/* Cursor shifts to the left */ /* Cursor shifts to the right */ /* Display shifts to the left */ /* Display shifts to the right */

/* 4-bit Interface */ /* 8-bit Interface */ /* 5x7 characters, single line */ /* 5x10 characters */ /* 5x7 characters, multiple line */

#define PARAM_SCLASS auto #define MEM_MODEL far /* Change this to near for small memory model */ /* OpenXLCD * Configures I/O pins for external LCD */ void OpenXLCD(PARAM_SCLASS unsigned char); /* SetCGRamAddr * Sets the character generator address */ void SetCGRamAddr(PARAM_SCLASS unsigned char); /* SetDDRamAddr * Sets the display data address */ void SetDDRamAddr(PARAM_SCLASS unsigned char); /* BusyXLCD * Returns the busy status of the LCD */ unsigned char BusyXLCD(void); /* ReadAddrXLCD * Reads the current address */ unsigned char ReadAddrXLCD(void); /* ReadDataXLCD * Reads a byte of data */ char ReadDataXLCD(void); /* WriteCmdXLCD * Writes a command to the LCD */
2004 -2008 Sure Electronics Inc. DB-DP113_Ver2.0_Page47

PICDEM 2 PLUS SAMPLE CODE


void WriteCmdXLCD(PARAM_SCLASS unsigned char); /* WriteDataXLCD * Writes a data byte to the LCD */ void WriteDataXLCD(PARAM_SCLASS char); /* putcXLCD * A putc is a write */ #define putcXLCD WriteDataXLCD /* putsXLCD * Writes a string of characters to the LCD */ void putsXLCD(PARAM_SCLASS char *); /* putrsXLCD * Writes a string of characters in ROM to the LCD */ void putrsXLCD(PARAM_SCLASS const MEM_MODEL rom char *); /* User defines these routines according to the oscillator frequency */ extern void DelayFor18TCY(void); extern void DelayPORXLCD(void); extern void DelayXLCD(void);

/******************************************************************** * Function Name: BusyXLCD * * Return Value: char: busy status of LCD controller * * Parameters: void * * Description: This routine reads the busy status of the * * Hitachi HD44780 LCD controller. * ********************************************************************/ unsigned char BusyXLCD(void) { RW_PIN = 1; // Set the control bits for read RS_PIN = 0; DelayFor18TCY(); E_PIN = 1; // Clock in the command DelayFor18TCY(); #ifdef BIT8 // 8-bit interface if(DATA_PORT&0x80) // Read bit 7 (busy bit) { // If high E_PIN = 0; // Reset clock line RW_PIN = 0; // Reset control line return 1; // Return TRUE } else // Bit 7 low
DB-DP113_Ver2.0_Page48 2004 -2008 Sure Electronics Inc.

PICDEM 2 PLUS SAMPLE CODE


{ E_PIN = 0; RW_PIN = 0; return 0; } #else #ifdef UPPER if(DATA_PORT&0x80) #else if(DATA_PORT&0x08) #endif { E_PIN = 0; DelayFor18TCY(); E_PIN = 1; DelayFor18TCY(); E_PIN = 0; RW_PIN = 0; return 1; } else { E_PIN = 0; DelayFor18TCY(); E_PIN = 1; DelayFor18TCY(); E_PIN = 0; RW_PIN = 0; return 0; } #endif } // Reset clock line // Reset control line // Return FALSE // 4-bit interface // Upper nibble interface // Lower nibble interface

// Reset clock line // Clock out other nibble

// Reset control line // Return TRUE // Busy bit is low // Reset clock line // Clock out other nibble

// Reset control line // Return FALSE

/******************************************************************** * Function Name: OpenXLCD * Return Value: void * * Parameters: lcdtype: sets the type of LCD (lines) * * Description: This routine configures the LCD. Based on * * the Hitachi HD44780 LCD controller. The * * routine will configure the I/O pins of the * * microcontroller, setup the LCD for 4-bit or * * 8-bit mode and clear screen. The user * * must provide three delay routines: * * DelayFor18TCY() provides a 18 Tcy delay * * DelayPORXLCD() provides at least 15ms delay * * DelayXLCD() provides at least 5ms delay * ********************************************************************/ void OpenXLCD(unsigned char lcdtype) {
2004 -2008 Sure Electronics Inc.

DB-DP113_Ver2.0_Page49

PICDEM 2 PLUS SAMPLE CODE


// The data bits must be either a 8-bit port or the upper or // lower 4-bits of a port. These pins are made into inputs #ifdef BIT8 // 8-bit mode, use whole port DATA_PORT &= 0; TRIS_DATA_PORT |= 0xff; #else // 4-bit mode #ifdef UPPER // Upper 4-bits of the port DATA_PORT &= 0x0f; TRIS_DATA_PORT |= 0xf0; #else // Lower 4-bits of the port DATA_PORT &= 0xf0; TRIS_DATA_PORT |= 0x0f; #endif #endif TRIS_RW = 0; // All control signals made outputs TRIS_RS = 0; TRIS_E = 0; RW_PIN = 0; // R/W pin made low RS_PIN = 0; // Register select pin made low E_PIN = 0; // Clock pin made low // Delay for 15ms to allow for LCD Power on reset DelayPORXLCD(); // Setup interface to LCD #ifdef BIT8 TRIS_DATA_PORT &= 0; DATA_PORT &= 0; DATA_PORT |= 0b00110000; #else #ifdef UPPER TRIS_DATA_PORT &= 0x0f; DATA_PORT &= 0x0f; DATA_PORT |= 0b00100000; #else TRIS_DATA_PORT &= 0xf0; DATA_PORT &= 0xf0; DATA_PORT |= 0b00000010; #endif #endif E_PIN = 1; DelayFor18TCY(); E_PIN = 0; // Delay for at least 4.1ms DelayXLCD(); // Setup interface to LCD #ifdef BIT8 DATA_PORT &= 0;
DB-DP113_Ver2.0_Page50

// 8-bit mode interface // Data port output // Function set cmd(8-bit interface) // 4-bit mode interface // Upper nibble interface

// Function set cmd(4-bit interface) // Lower nibble interface

// Function set cmd(4-bit interface)

// Clock the cmd in

// 8-bit interface
2004 -2008 Sure Electronics Inc.

PICDEM 2 PLUS SAMPLE CODE


DATA_PORT |= 0b00110000; #else #ifdef UPPER DATA_PORT &= 0x0f; DATA_PORT |= 0b00100000; #else DATA_PORT &= 0xf0; DATA_PORT |= 0b00000010; #endif #endif E_PIN = 1; DelayFor18TCY(); E_PIN = 0; // Delay for at least 100us DelayXLCD(); // Setup interface to LCD #ifdef BIT8 DATA_PORT &= 0; DATA_PORT |= 0b00110000; #else #ifdef UPPER DATA_PORT &= 0x0f; DATA_PORT |= 0b00100000; #else DATA_PORT &= 0xf0; DATA_PORT |= 0b00000010; #endif #endif E_PIN = 1; DelayFor18TCY(); E_PIN = 0; #ifdef BIT8 TRIS_DATA_PORT |= 0xff; #else #ifdef UPPER TRIS_DATA_PORT |= 0xf0; #else TRIS_DATA_PORT |= 0x0f; #endif #endif // Function set cmd(8-bit interface) // 4-bit interface // Upper nibble interface // Function set cmd(4-bit interface) // Lower nibble interface // Function set cmd(4-bit interface)

// Clock the cmd in

// 8-bit interface // Function set cmd(8-bit interface) // 4-bit interface // Upper nibble interface // Function set cmd(4-bit interface) // Lower nibble interface // Function set cmd(4-bit interface)

// Clock cmd in

// 8-bit interface // Make data port input // 4-bit interface // Upper nibble interface // Make data nibble input // Lower nibble interface // Make data nibble input

// Set data interface width, # lines, font while(BusyXLCD()); // Wait if LCD busy WriteCmdXLCD(lcdtype); // Function set cmd // Turn the display on then off
2004 -2008 Sure Electronics Inc. DB-DP113_Ver2.0_Page51

PICDEM 2 PLUS SAMPLE CODE


while(BusyXLCD()); // Wait if LCD busy WriteCmdXLCD(DOFF); // Display OFF/Blink OFF while(BusyXLCD()); // Wait if LCD busy WriteCmdXLCD(DON); // Display ON/Blink ON while(BusyXLCD()); // Wait if LCD busy WriteCmdXLCD(BLINK_OFF&CURSOR_OFF); // Display ON/Blink OFF // Clear display while(BusyXLCD()); WriteCmdXLCD(0x01);

// Wait if LCD busy // Clear display

// Set entry mode inc, no shift while(BusyXLCD()); // Wait if LCD busy WriteCmdXLCD(SHIFT_CUR_LEFT); // Entry Mode // Set DD Ram address to 0 while(BusyXLCD()); SetDDRamAddr(0); return; }

// Wait if LCD busy // Set Display data ram address to 0

/******************************************************************** * Function Name: putrsXLCD * Return Value: void * Parameters: buffer: pointer to string * Description: This routine writes a string of bytes to the * Hitachi HD44780 LCD controller. The user * must check to see if the LCD controller is * busy before calling this routine. The data * is written to the character generator RAM or * the display data RAM depending on what the * previous SetxxRamAddr routine was called. ********************************************************************/ /* void putrsXLCD(const rom char *buffer) { while(*buffer) // Write data to LCD up to null { while(BusyXLCD()); // Wait while LCD is busy WriteDataXLCD(*buffer); // Write character to LCD buffer++; // Increment buffer } return; } */ /********************************************************************
DB-DP113_Ver2.0_Page52 2004 -2008 Sure Electronics Inc.

PICDEM 2 PLUS SAMPLE CODE


* Function Name: putsXLCD * Return Value: void * Parameters: buffer: pointer to string * Description: This routine writes a string of bytes to the * Hitachi HD44780 LCD controller. The user * must check to see if the LCD controller is * busy before calling this routine. The data * is written to the character generator RAM or * the display data RAM depending on what the * previous SetxxRamAddr routine was called. ********************************************************************/ void putsXLCD(char *buffer) { while(*buffer) // Write data to LCD up to null { while(BusyXLCD()); // Wait while LCD is busy WriteDataXLCD(*buffer); // Write character to LCD buffer++; // Increment buffer } return; }

/********************************************************************* * Function Name: ReadAddrXLCD * * Return Value: char: address from LCD controller * * Parameters: void * * Description: This routine reads an address byte from the * * Hitachi HD44780 LCD controller. The user * * must check to see if the LCD controller is * * busy before calling this routine. The address* * is read from the character generator RAM or * * the display data RAM depending on what the * * previous SetxxRamAddr routine was called. * *********************************************************************/ unsigned char ReadAddrXLCD(void) { char data; // Holds the data retrieved from the LCD #ifdef BIT8 RW_PIN = 1; RS_PIN = 0; DelayFor18TCY(); E_PIN = 1; DelayFor18TCY(); data = DATA_PORT; E_PIN = 0; RW_PIN = 0; #else
2004 -2008 Sure Electronics Inc.

// 8-bit interface // Set control bits for the read

// Clock data out of the LCD controller // Save the data in the register // Reset the control bits // 4-bit interface
DB-DP113_Ver2.0_Page53

PICDEM 2 PLUS SAMPLE CODE


RW_PIN = 1; // Set control bits for the read RS_PIN = 0; DelayFor18TCY(); E_PIN = 1; // Clock data out of the LCD controller DelayFor18TCY(); #ifdef UPPER // Upper nibble interface data = DATA_PORT&0xf0; // Read the nibble into the upper nibble of data #else // Lower nibble interface data = (DATA_PORT<<4)&0xf0; // Read the nibble into the upper nibble of data #endif E_PIN = 0; // Reset the clock DelayFor18TCY(); E_PIN = 1; // Clock out the lower nibble DelayFor18TCY(); #ifdef UPPER // Upper nibble interface data |= (DATA_PORT>>4)&0x0f; // Read the nibble into the lower nibble of data #else // Lower nibble interface data |= DATA_PORT&0x0f; // Read the nibble into the lower nibble of data #endif E_PIN = 0; RW_PIN = 0; // Reset the control lines #endif return (data&0x7f); // Return the address, Mask off the busy bit }

/******************************************************************** * Function Name: ReadDataXLCD * Return Value: char: data byte from LCD controller * * Parameters: void * * Description: This routine reads a data byte from the * * Hitachi HD44780 LCD controller. The user * * must check to see if the LCD controller is * * busy before calling this routine. The data * * is read from the character generator RAM or * * the display data RAM depending on what the * * previous SetxxRamAddr routine was called. * ********************************************************************/ char ReadDataXLCD(void) { char data; #ifdef BIT8 RS_PIN = 1; RW_PIN = 1; DelayFor18TCY();
DB-DP113_Ver2.0_Page54

// 8-bit interface // Set the control bits

2004 -2008 Sure Electronics Inc.

PICDEM 2 PLUS SAMPLE CODE


E_PIN = 1; DelayFor18TCY(); data = DATA_PORT; E_PIN = 0; RS_PIN = 0; RW_PIN = 0; #else // Clock the data out of the LCD // Read the data // Reset the control bits

// 4-bit interface RW_PIN = 1; RS_PIN = 1; DelayFor18TCY(); E_PIN = 1; // Clock the data out of the LCD DelayFor18TCY(); #ifdef UPPER // Upper nibble interface data = DATA_PORT&0xf0; // Read the upper nibble of data #else // Lower nibble interface data = (DATA_PORT<<4)&0xf0; // read the upper nibble of data #endif E_PIN = 0; // Reset the clock line DelayFor18TCY(); E_PIN = 1; // Clock the next nibble out of the LCD DelayFor18TCY(); #ifdef UPPER // Upper nibble interface data |= (DATA_PORT>>4)&0x0f; // Read the lower nibble of data #else // Lower nibble interface data |= DATA_PORT&0x0f; // Read the lower nibble of data #endif E_PIN = 0; RS_PIN = 0; // Reset the control bits RW_PIN = 0; #endif return(data); // Return the data byte }

/******************************************************************** * Function Name: SetCGRamAddr * * Return Value: void * * Parameters: CGaddr: character generator ram address * * Description: This routine sets the character generator * * address of the Hitachi HD44780 LCD * * controller. The user must check to see if * * the LCD controller is busy before calling * * this routine. * ********************************************************************/ void SetCGRamAddr(unsigned char CGaddr) { #ifdef BIT8 // 8-bit interface TRIS_DATA_PORT = 0; // Make data port ouput DATA_PORT = CGaddr | 0b01000000; // Write cmd and address to
2004 -2008 Sure Electronics Inc. DB-DP113_Ver2.0_Page55

PICDEM 2 PLUS SAMPLE CODE


port RW_PIN = 0; RS_PIN = 0; DelayFor18TCY(); E_PIN = 1; DelayFor18TCY(); E_PIN = 0; DelayFor18TCY(); TRIS_DATA_PORT = 0xff; // Set control signals

// Clock cmd and address in

// Make data port inputs #else // 4-bit interface #ifdef UPPER // Upper nibble interface TRIS_DATA_PORT &= 0x0f; // Make nibble input DATA_PORT &= 0x0f; // and write upper nibble DATA_PORT |= ((CGaddr | 0b01000000) & 0xf0); #else // Lower nibble interface TRIS_DATA_PORT &= 0xf0; // Make nibble input DATA_PORT &= 0xf0; // and write upper nibble DATA_PORT |= (((CGaddr |0b01000000)>>4) & 0x0f); #endif RW_PIN = 0; // Set control signals RS_PIN = 0; DelayFor18TCY(); E_PIN = 1; // Clock cmd and address in DelayFor18TCY(); E_PIN = 0; #ifdef UPPER // Upper nibble interface DATA_PORT &= 0x0f; // Write lower nibble DATA_PORT |= ((CGaddr<<4)&0xf0); #else // Lower nibble interface DATA_PORT &= 0xf0; // Write lower nibble DATA_PORT |= (CGaddr&0x0f); #endif DelayFor18TCY(); E_PIN = 1; // Clock cmd and address in DelayFor18TCY(); E_PIN = 0; #ifdef UPPER // Upper nibble interface TRIS_DATA_PORT |= 0xf0; // Make inputs #else // Lower nibble interface TRIS_DATA_PORT |= 0x0f; // Make inputs #endif #endif return; }

/******************************************************************** * Function Name: SetDDRamAddr * Return Value: void * Parameters: CGaddr: display data address
DB-DP113_Ver2.0_Page56

* * *

2004 -2008 Sure Electronics Inc.

PICDEM 2 PLUS SAMPLE CODE


* Description: This routine sets the display data address * * of the Hitachi HD44780 LCD controller. The * * user must check to see if the LCD controller* * is busy before calling this routine. * ********************************************************************/ void SetDDRamAddr(unsigned char DDaddr) { #ifdef BIT8 // 8-bit interface TRIS_DATA_PORT = 0; // Make port output DATA_PORT = DDaddr | 0b10000000; // Write cmd and address to port RW_PIN = 0; // Set the control bits RS_PIN = 0; DelayFor18TCY(); E_PIN = 1; // Clock the cmd and address in DelayFor18TCY(); E_PIN = 0; DelayFor18TCY(); TRIS_DATA_PORT = 0xff; // Make port input #else // 4-bit interface #ifdef UPPER // Upper nibble interface TRIS_DATA_PORT &= 0x0f; // Make port output DATA_PORT &= 0x0f; // and write upper nibble DATA_PORT |= ((DDaddr | 0b10000000) & 0xf0); #else // Lower nibble interface TRIS_DATA_PORT &= 0xf0; // Make port output DATA_PORT &= 0xf0; // and write upper nibble DATA_PORT |= (((DDaddr | 0b10000000)>>4) & 0x0f); #endif RW_PIN = 0; // Set control bits RS_PIN = 0; DelayFor18TCY(); E_PIN = 1; // Clock the cmd and address in DelayFor18TCY(); E_PIN = 0; #ifdef UPPER // Upper nibble interface DATA_PORT &= 0x0f; // Write lower nibble DATA_PORT |= ((DDaddr<<4)&0xf0); #else // Lower nibble interface DATA_PORT &= 0xf0; // Write lower nibble DATA_PORT |= (DDaddr&0x0f); #endif DelayFor18TCY(); E_PIN = 1; // Clock the cmd and address in DelayFor18TCY(); E_PIN = 0; #ifdef UPPER // Upper nibble interface TRIS_DATA_PORT |= 0xf0; // Make port input #else // Lower nibble interface
2004 -2008 Sure Electronics Inc. DB-DP113_Ver2.0_Page57

PICDEM 2 PLUS SAMPLE CODE


TRIS_DATA_PORT |= 0x0f; #endif #endif return; } // Make port input

/******************************************************************** * Function Name: WriteCmdXLCD * * Return Value: void * * Parameters: cmd: command to send to LCD * * Description: This routine writes a command to the Hitachi* * HD44780 LCD controller. The user must check * * to see if the LCD controller is busy before * * calling this routine. * ********************************************************************/ void WriteCmdXLCD(unsigned char cmd) { #ifdef BIT8 // 8-bit interface TRIS_DATA_PORT &= 0; // Data port output DATA_PORT &= 0; DATA_PORT |= cmd; // Write command to data port RW_PIN = 0; // Set the control signals RS_PIN = 0; // for sending a command DelayFor18TCY(); E_PIN = 1; // Clock the command in DelayFor18TCY(); E_PIN = 0; DelayFor18TCY(); TRIS_DATA_PORT |= 0xff; // Data port input #else // 4-bit interface #ifdef UPPER // Upper nibble interface TRIS_DATA_PORT &= 0x0f; DATA_PORT &= 0x0f; DATA_PORT |= cmd&0xf0; #else // Lower nibble interface TRIS_DATA_PORT &= 0xf0; DATA_PORT &= 0xf0; DATA_PORT |= (cmd>>4)&0x0f; #endif RW_PIN = 0; // Set control signals for command RS_PIN = 0; DelayFor18TCY(); E_PIN = 1; // Clock command in DelayFor18TCY(); E_PIN = 0; #ifdef UPPER // Upper nibble interface DATA_PORT &= 0x0f; DATA_PORT |= (cmd<<4)&0xf0; #else // Lower nibble interface
DB-DP113_Ver2.0_Page58 2004 -2008 Sure Electronics Inc.

PICDEM 2 PLUS SAMPLE CODE


DATA_PORT &= 0xf0; DATA_PORT |= cmd&0x0f; #endif DelayFor18TCY(); E_PIN = 1; DelayFor18TCY(); E_PIN = 0; #ifdef UPPER TRIS_DATA_PORT |= 0xf0; #else TRIS_DATA_PORT |= 0x0f; #endif #endif return; } // Clock command in

// Make data nibble input

/******************************************************************** * Function Name: WriteDataXLCD * * Return Value: void * * Parameters: data: data byte to be written to LCD * * Description: This routine writes a data byte to the * * Hitachi HD44780 LCD controller. The user * * must check to see if the LCD controller is * * busy before calling this routine. The data * * is written to the character generator RAM or* * the display data RAM depending on what the * * previous SetxxRamAddr routine was called. * ********************************************************************/ void WriteDataXLCD(char data) { #ifdef BIT8 // 8-bit interface TRIS_DATA_PORT = 0; // Make port output DATA_PORT = data; // Write data to port RS_PIN = 1; // Set control bits RW_PIN = 0; DelayFor18TCY(); E_PIN = 1; // Clock data into LCD DelayFor18TCY(); E_PIN = 0; RS_PIN = 0; // Reset control bits TRIS_DATA_PORT = 0xff; // Make port input #else // 4-bit interface #ifdef UPPER // Upper nibble interface TRIS_DATA_PORT &= 0x0f; DATA_PORT &= 0x0f; DATA_PORT |= data&0xf0; #else // Lower nibble interface TRIS_DATA_PORT &= 0xf0;
2004 -2008 Sure Electronics Inc. DB-DP113_Ver2.0_Page59

PICDEM 2 PLUS SAMPLE CODE


DATA_PORT &= 0xf0; DATA_PORT |= ((data>>4)&0x0f); #endif RS_PIN = 1; RW_PIN = 0; DelayFor18TCY(); E_PIN = 1; DelayFor18TCY(); E_PIN = 0; #ifdef UPPER DATA_PORT &= 0x0f; DATA_PORT |= ((data<<4)&0xf0); #else DATA_PORT &= 0xf0; DATA_PORT |= (data&0x0f); #endif DelayFor18TCY(); E_PIN = 1; DelayFor18TCY(); E_PIN = 0; #ifdef UPPER TRIS_DATA_PORT |= 0xf0; #else TRIS_DATA_PORT |= 0x0f; #endif #endif return; } void DelayFor18TCY( void ) { Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); } void DelayPORXLCD (void) { Delay1KTCYx(75); // Delay of 15ms // Cycles = (TimeDelay * Fosc) / 4 // Cycles = (15ms * 20MHz) / 4 // Cycles = 75,000
DB-DP113_Ver2.0_Page60 2004 -2008 Sure Electronics Inc.

// Set control bits

// Clock nibble into LCD

// Upper nibble interface

// Lower nibble interface

// Clock nibble into LCD

// Upper nibble interface // Lower nibble interface

PICDEM 2 PLUS SAMPLE CODE


return; } void DelayXLCD (void) { Delay1KTCYx(25); // Delay of 5ms // Cycles = (TimeDelay * Fosc) / 4 // Cycles = (5ms * 20MHz) / 4 // Cycles = 25,000 return; } void delayinit(void) { //Delay of 4s Delay10KTCYx(200); Delay10KTCYx(200); Delay10KTCYx(200); Delay10KTCYx(200); Delay10KTCYx(200); Delay10KTCYx(200); Delay10KTCYx(200); Delay10KTCYx(200); Delay10KTCYx(200); Delay10KTCYx(200); } void LCD_init(void) { OpenXLCD( EIGHT_BIT&LINES_5X7 );// Configure External LCD while(BusyXLCD()); putsXLCD(LCD_name); while(BusyXLCD()); SetDDRamAddr(0x40); putsXLCD(LCD_Ver); delayinit(); } void LCD_display(char *i) { // Set DD Ram address to 0 while(BusyXLCD()); SetDDRamAddr(0x40); // write to LCD while(BusyXLCD()); putsXLCD(i); }
2004 -2008 Sure Electronics Inc. DB-DP113_Ver2.0_Page61

// Wait if LCD is busy // write to LCD // Wait if LCD is busy // Set Display data RAM address to 0x40 // write to LCD

// Wait if LCD is busy // Set Display data RAM address

// Wait if LCD is busy

PICDEM 2 PLUS SAMPLE CODE


/**************************************************************************/ #pragma code low_vector=0x18 void low_interrupt (void) { _asm GOTO timer_isr _endasm } #pragma code #pragma interruptlow timer_isr void timer_isr (void) { unsigned int i,m,n; unsigned char send; char Cent[]="Centigrade"; char Fahr[]="Fahrenheit"; char Cont[]="Contrast "; char Current[]="Current Temperature"; TMR0H=0X80; TMR0L=0X00; i++; LM75_temperature(); //Read Temperature

if(key_flag==0) { m=Funckey(); switch(m) { case(0): //Display "Centigrade" { LCD_display(Cent); putsXLCD(cent_buf); PWM_data=m; }break; case(1): //Display "Fahrenheit" { LCD_display(Fahr); putsXLCD(fahr_buf); PWM_data=m; }break; case(2): //Display Contrast information { LCD_display(Cont); PWM_data=m; }break; } if(change_key==0)key_flag=1; }
DB-DP113_Ver2.0_Page62 2004 -2008 Sure Electronics Inc.

PICDEM 2 PLUS SAMPLE CODE


if(key_flag==1) { n=changekey();

//Get state of change function key

if((PWM_data==2)|(PWM_data==3)) { PWM(n); switch(n) { case(0): { PWMbuf[0]='0'; PWMbuf[1]='%'; PWMbuf[2]=' '; PWMbuf[3]=' '; PWMbuf[4]=' '; PWMbuf[5]=' '; while(BusyXLCD()); SetDDRamAddr(0x4b); putsXLCD(PWMbuf); }break; case(1): { PWMbuf[0]='1'; PWMbuf[1]='0'; PWMbuf[2]='%'; PWMbuf[3]=' '; PWMbuf[4]=' '; PWMbuf[5]=' '; while(BusyXLCD()); SetDDRamAddr(0x4b); putsXLCD(PWMbuf); }break; case(2): { PWMbuf[0]='2'; PWMbuf[1]='0'; PWMbuf[2]='%'; PWMbuf[3]=' '; PWMbuf[4]=' '; PWMbuf[5]=' '; while(BusyXLCD()); SetDDRamAddr(0x4b); putsXLCD(PWMbuf); }break; case(3): { PWMbuf[0]='3'; PWMbuf[1]='0';
2004 -2008 Sure Electronics Inc.

// Wait if LCD is busy // Set Display data RAM address

// Wait if LCD is busy // Set Display data RAM address

// Wait if LCD is busy // Set Display data RAM address

DB-DP113_Ver2.0_Page63

PICDEM 2 PLUS SAMPLE CODE


PWMbuf[2]='%'; PWMbuf[3]=' '; PWMbuf[4]=' '; PWMbuf[5]=' '; while(BusyXLCD()); SetDDRamAddr(0x4b); putsXLCD(PWMbuf); }break; case(4): { PWMbuf[0]='4'; PWMbuf[1]='0'; PWMbuf[2]='%'; PWMbuf[3]=' '; while(BusyXLCD()); SetDDRamAddr(0x4b); putsXLCD(PWMbuf); }break; case(5): { PWMbuf[0]='5'; PWMbuf[1]='0'; PWMbuf[2]='%'; PWMbuf[3]=' '; PWMbuf[4]=' '; PWMbuf[5]=' '; while(BusyXLCD()); SetDDRamAddr(0x4b); putsXLCD(PWMbuf); }break; case(6): { PWMbuf[0]='6'; PWMbuf[1]='0'; PWMbuf[2]='%'; PWMbuf[3]=' '; PWMbuf[4]=' '; PWMbuf[5]=' '; while(BusyXLCD()); SetDDRamAddr(0x4b); putsXLCD(PWMbuf); }break; case(7): { PWMbuf[0]='7'; PWMbuf[1]='0'; PWMbuf[2]='%'; PWMbuf[3]=' '; PWMbuf[4]=' '; PWMbuf[5]=' ';
DB-DP113_Ver2.0_Page64

// Wait if LCD is busy // Set Display data RAM address

// Wait if LCD is busy // Set Display data RAM address

// Wait if LCD is busy // Set Display data RAM address

// Wait if LCD is busy // Set Display data ram address

2004 -2008 Sure Electronics Inc.

PICDEM 2 PLUS SAMPLE CODE


while(BusyXLCD()); SetDDRamAddr(0x4b); putsXLCD(PWMbuf); }break; case(8): { PWMbuf[0]='8'; PWMbuf[1]='0'; PWMbuf[2]='%'; PWMbuf[3]=' '; PWMbuf[4]=' '; PWMbuf[5]=' '; while(BusyXLCD()); SetDDRamAddr(0x4b); putsXLCD(PWMbuf); PWM(8); }break; case(9): { PWMbuf[0]='9'; PWMbuf[1]='0'; PWMbuf[2]='%'; PWMbuf[3]=' '; PWMbuf[4]=' '; PWMbuf[5]=' '; while(BusyXLCD()); SetDDRamAddr(0x4b); putsXLCD(PWMbuf); }break; case(10): { PWMbuf[0]='1'; PWMbuf[1]='0'; PWMbuf[2]='0'; PWMbuf[3]='%'; PWMbuf[4]=' '; PWMbuf[5]=' '; while(BusyXLCD()); SetDDRamAddr(0x4b); putsXLCD(PWMbuf); }break; } } if(Func_key==0)key_flag=0; } if(i%8==0) { send++;
2004 -2008 Sure Electronics Inc. DB-DP113_Ver2.0_Page65

// Wait if LCD is busy // Set Display data RAM address

// Wait if LCD is busy // Set Display data RAM address

// Wait if LCD is busy // Set Display data RAM address

// Wait if LCD is busy // Set Display data RAM address

PICDEM 2 PLUS SAMPLE CODE


if((send>0)&(send<24)) { sendUSART(Current); } if(send==24) { pointer=0; TXREG=13; TXREG=10; } if((send>28)&(send<36)) { sendUSART(cent_buf); } if(send==36) { pointer=0; } if((send>36)&(send<52)) { sendUSART(Cent); } if(send==52) { pointer=0; TXREG=13; TXREG=10; } if((send>56)&(send<64)) { sendUSART(Fahr); } if(send==64) { pointer=0; } if((send>64)&(send<80)) { sendUSART(Fahr); } if(send==80) { pointer=0; TXREG=13; TXREG=10; } } INTCONbits.TMR0IF = 0; }
DB-DP113_Ver2.0_Page66 2004 -2008 Sure Electronics Inc.

PICDEM 2 PLUS SAMPLE CODE


void sendUSART(char *sendbuf) { if(*(sendbuf+pointer)!=0) { if(BusyUSART()==0) { TXREG=*(sendbuf+pointer); pointer++; return; } else { return; } } } /**************************************************************************/ void main (void) { init(); // Initialize Control Microchip LM75_init(); // Temperature Sensor Initializtion LM75_temperature(); // Read Temperature LCD_init(); // Open LCD and display "Sure Electronics" and "Ver 2.1" OpenTimer0 (TIMER_INT_ON & T0_SOURCE_INT & T0_16BIT); INTCONbits.GIE = 1; while(1){}; }

2004 -2008 Sure Electronics Inc.

DB-DP113_Ver2.0_Page67

Vous aimerez peut-être aussi