Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "msp430g2553.h"
- #define DIR P2DIR
- #define POUT P2OUT
- //following maps bits to lcd pins
- //note: remember to set p5 to gnd to set r/w to data write
- #define RS BIT5 //p4;; high sets data input;; low sets instruction input
- #define EN BIT4 //p6;; pulled high to enable device
- #define D4 BIT0 //p11
- #define D5 BIT1 //p12
- #define D6 BIT2 //p13
- #define D7 BIT3 //p14
- #define PMSK (RS|EN|D7|D6|D5|D4)
- #define FALSE 0
- #define TRUE 1
- void enable(){
- //POUT &= ~EN; //pulls en bit low
- //__delay_cycles(5000);
- POUT |= EN; //pulls en bit high
- __delay_cycles(200);
- POUT &= ~EN;
- __delay_cycles(200);
- }
- //function to send a byte to the display
- //might just use bool as second argument of this function
- void sendbyte(char bsend, int isdata){
- POUT &= ~PMSK; //clears all output pins
- //now need to send high nibble first
- //right shift 4 due to xmitting on pins 0->3
- POUT |=((bsend >> 4) & 0x0F);
- if(isdata==TRUE){
- POUT |= RS;
- }
- else{
- POUT &= (~RS);
- }
- enable(); //sends enable signal.
- POUT &= ~PMSK; //clears all output pins
- //now need to send low nibble
- //no shifting required, already lined up.
- POUT |= (bsend & 0x0F);
- if(isdata==TRUE){
- POUT |= RS;
- }
- else{
- POUT &= (~RS);
- }
- enable();
- }
- //the following function sets the cursor position
- void setcursor(char row, char col){
- char address;
- //make address from row, col coords.
- if(row==0){
- address=0;
- }
- else{
- address=0x40; //address of the second line in memory
- }
- address |= col; //sets the offset from the row
- sendbyte((0x80|address), FALSE);
- }
- //clears the screen and returns cursor to home
- void clrscreen(){
- sendbyte(0x01, FALSE);
- sendbyte(0x02, FALSE);
- }
- //initialization subroutine, only run once on startup
- void init(void){
- P1DIR |= 0x01;
- P1OUT = 0x00;
- DIR |= PMSK; //sets all pmask pins to output
- POUT &= (~PMSK); //sets all pins low
- __delay_cycles(1000000); //lcd needs delay to power up
- //following are magical commands to reset for 4 bit mode..
- //set to 8 bit 3 times then initialize as 4 bit
- POUT |= 0x03;
- enable();
- enable();
- enable();
- //send 4 bit input - second time? supposedly but
- POUT &= ~PMSK;
- POUT |= (0x02);
- enable();
- sendbyte(0x28, FALSE);
- //display on
- sendbyte(0x01, FALSE);
- //set cursor behavior (display and blink)
- sendbyte(0x0F, FALSE);
- //allow cursor auto increment
- sendbyte(0x06, FALSE);
- //set to home
- sendbyte(0x02, FALSE);
- }
- void printstr(char* str){
- char* c;
- c=str;
- while((c!=0)&&(*c!=00)){
- sendbyte(*c, TRUE);
- c++;
- P1OUT ^= BIT0;
- }
- P1OUT &= ~BIT0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement