Académique Documents
Professionnel Documents
Culture Documents
DB-DP113_Ver1.0
DB-DP113_Ver1.0_Page1
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.
// Close Comparator
// Initialize Microchip
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
DB-DP113_Ver2.0_Page3
// Initializtion
//Buzzer Beep
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.
// Memory Centigrade and Fahrenheit value // Centigrade and Fahrenheit value array
// Close Comparator
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
// Close Comparator // RA4 is for Switch SW1 Input // RB0 is for Switch SW2 Input
// 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(); }
// 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.
// 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
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
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
// Close Comparator // RA4 is for Switch SW1 Input // RB0 is for Switch SW2 Input
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
/* 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
/******************************************************************** * 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.
// Reset control line // Return TRUE // Busy bit is low // Reset clock line // Clock out other nibble
/******************************************************************************************* * 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
// 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
// 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.
// 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
// 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; }
/******************************************************************** * 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.
/********************************************************************* * 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.
// 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
/******************************************************************** * 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
// 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
// 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
/******************************************************************** * 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.
/******************************************************************** * 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
// Close Comparator
// 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){}; }
DB-DP113_Ver2.0_Page29
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
// 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.
// 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
// Control Byte
2004 -2008 Sure Electronics Inc.
// Word Address
// Control Byte
// Control Byte
// Write Data
DB-DP113_Ver2.0_Page33
//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
//Display "Sure"
//Display "1.1"
//Enter //Newline
DB-DP113_Ver2.0_Page39
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.
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
// Baud Rate 4800bps // Choose 8-bit Baud Rate Generator // High Baud Rate // Asynchronous Mode // Enable Serial // 8-bit Transmission // Enable Transmission // Set PWM Mode
// 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.
// 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
/* Display on */ /* Display off */ /* Cursor on */ /* Cursor off */ /* Cursor Blink */ /* Cursor No Blink */
2004 -2008 Sure Electronics Inc.
/* 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
/******************************************************************** * 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.
// Reset control line // Return TRUE // Busy bit is low // Reset clock line // Clock out other nibble
/******************************************************************** * 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
// 8-bit mode interface // Data port output // Function set cmd(8-bit interface) // 4-bit mode interface // Upper nibble interface
// 8-bit interface
2004 -2008 Sure Electronics Inc.
// 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
// 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; }
/******************************************************************** * 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.
/********************************************************************* * 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.
// 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
/******************************************************************** * 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
// 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
// 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
* * *
/******************************************************************** * 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.
/******************************************************************** * 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
// Wait if LCD is busy // write to LCD // Wait if LCD is busy // Set Display data RAM address to 0x40 // write to LCD
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.
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.
DB-DP113_Ver2.0_Page63
DB-DP113_Ver2.0_Page67