Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define WDTCL *((short *)0x120)
- #define WDTHOLD 0x0080
- #define WDTPW 0x5a00
- #define LCD_Data P2OUT
- #define _100us 100 //
- #define _10us 10 //
- #define E 3 //P2.3
- #define RS 2 //P2.2
- #define CLR_DISP 0x01 // clear display
- #define CUR_HOME 0x02 // return home
- #define ENTRY_INC 0x06 // entry mode increment
- #define ENTRY_INC_ROL 0x07 // entry mode increment with rol data
- #define ENTRY_DEC 0x04 // entry mode decrement
- #define ENTRY_DEC_ROL 0x05 // entry mode decrement witch rol dat
- #define DISP_OFF 0x08 // all display off
- #define DISP_ON 0x0c // all display on
- #define DATA_ROL_LEFT 0x18 // rol data left
- #define DATA_ROL_RIGHT 0x1c // rol data right
- #define CUR_SHIFT_LEFT 0x10 // shift coursor left
- #define CUR_SHIFT_RIGHT 0x14 // shift coursor right
- #define DD_RAM_ADDR 0x80 // set DD_RAM
- #define DD_RAM_ADDR2 0xc0 // set CG_RAM
- #define DD_RAM_ADDR3 0x28 //
- #define CG_RAM_ADDR 0x40 //
- #include "msp430x14x.h"
- #define bitset(var,bitno) ((var) |= 1 << (bitno))
- #define bitclr(var,bitno) ((var) &= ~(1 << (bitno)))
- void Delay (unsigned int a);
- void Delayx100us(unsigned char b);
- void _E(void);
- void SEND_CMD (unsigned char e) {
- int temp;
- Delayx100us(10); //10ms
- temp = e & 0xf0; //get upper nibble
- LCD_Data &= 0x0f;
- LCD_Data |= temp; //send CMD to LCD
- bitclr(P2OUT,RS); //set LCD to CMD mode
- _E(); //toggle E for LCD
- temp = e & 0x0f;
- temp = temp << 4; //get down nibble
- LCD_Data &= 0x0f;
- LCD_Data |= temp;
- bitclr(P2OUT,RS); //set LCD to CMD mode
- _E(); //toggle E for LCD
- }
- void clearDisplay() {
- SEND_CMD(CLR_DISP);
- Delayx100us(10);
- }
- void Delay (unsigned int a) {
- int k;
- for (k=0 ; k != a; ++k) {
- _NOP();
- _NOP();
- _NOP();
- _NOP();
- }
- }
- void Delayx100us(unsigned char b) {
- int j;
- for (j=0; j!=b; ++j) Delay (_100us);
- }
- void _E(void) {
- bitset(P2OUT,E); //toggle E for LCD
- Delay(_10us);
- bitclr(P2OUT,E);
- }
- void SEND_CHAR (unsigned char d) {
- int temp;
- Delayx100us(5); //.5ms
- temp = d & 0xf0; //get upper nibble
- LCD_Data &= 0x0f;
- LCD_Data |= temp;
- bitset(P2OUT,RS); //set LCD to data mode
- _E(); //toggle E for LCD
- temp = d & 0x0f;
- temp = temp << 4; //get down nibble
- LCD_Data &= 0x0f;
- LCD_Data |= temp;
- bitset(P2OUT,RS); //set LCD to data mode
- _E(); //toggle E for LCD
- }
- void InitPortsLcd(void) {
- P2SEL = 0;
- P2OUT = 0;
- P2DIR = ~BIT0; //only P2.0 is input
- }
- void InitLCD(void) {
- bitclr(P2OUT,RS);
- Delayx100us(250); //Delay 100ms
- Delayx100us(250);
- Delayx100us(250);
- Delayx100us(250);
- LCD_Data |= BIT4 | BIT5; //D7-D4 = 0011
- LCD_Data &= ~BIT6 & ~BIT7;
- _E(); //toggle E for LCD
- Delayx100us(100); //10ms
- _E(); //toggle E for LCD
- Delayx100us(100); //10ms
- _E(); //toggle E for LCD
- Delayx100us(100); //10ms
- LCD_Data &= ~BIT4;
- _E(); //toggle E for LCD
- SEND_CMD(DISP_ON);
- SEND_CMD(CLR_DISP);
- Delayx100us(250);
- Delayx100us(250);
- Delayx100us(250);
- Delayx100us(250);
- }
- void write(char *s, int n) {
- if(n == 2) SEND_CMD(DD_RAM_ADDR2 + 0x01);
- for(int i=0; i<strlen(s); i++) {
- SEND_CHAR(s[i]);
- }
- }
- void roll(char dir, int n) {
- for(int i=0; i<n; i++) {
- SEND_CMD( dir == 'L' ? DATA_ROL_LEFT : DATA_ROL_RIGHT );
- }
- }
- void writewrap(char *s, int shift) {
- SEND_CMD(CLR_DISP);
- SEND_CMD(DD_RAM_ADDR + shift);
- if(shift > 15) SEND_CMD(DD_RAM_ADDR2+shift-16);
- for(int i=0; i<strlen(s); i++) {
- if(i == 16-shift) SEND_CMD(DD_RAM_ADDR2);
- SEND_CHAR(s[i]);
- }
- }
- void writeint(int n) {
- char s[10];
- int i=0;
- int minus = n<0;
- if(minus) n = -n;
- do {
- s[i++] = (n % 10) + 48;
- n /= 10;
- } while(n);
- if(minus) {
- s[i] = '-';
- s[i+1] = 0;
- } else {
- s[i] = 0;
- }
- char s2[10];
- for(int i=0; i<strlen(s); i++) {
- s2[strlen(s)-i-1] = s[i];
- }
- s2[strlen(s)] = 0;
- writewrap(s2, 0);
- }
- int main(void) {
- WDTCL = WDTPW + WDTHOLD;
- InitPortsLcd();
- InitLCD();
- /*for(;;) {
- for(int i=0; i<37; i++) {
- writewrap("0123456789", i);
- Delayx100us(1000);
- }
- }*/
- writeint(0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement