- /***********************************************************
- 函數名稱:LCD_write_byte
- 函數功能:模擬SPI接口時序寫數據/命令LCD
- 入口參數:data :寫入的數據;
- command :寫數據/命令選擇;
- 出口參數:無
- 備 注:
- ***********************************************************/
- void LCD_write_byte(unsigned char dat, unsigned char command)
- {
- unsigned char i;
- LCD_CE = 0; //5110片選有效,允許輸入數據
- if (command == 0) //寫命令
- LCD_DC = 0;
- else
- LCD_DC = 1; //寫數據
- for(i=0;i<8;i++) //傳送8bit數據
- {
- if(dat&0x80)
- SDIN = 1;
- else
- SDIN = 0;
- SCLK = 0;
- dat = dat << 1;
- SCLK = 1;
- }
- LCD_CE = 1; //禁止5110
- }
- /***********************************************************
- 函數名稱:LCD_init
- 函數功能:5110初始化
- 入口參數:無
- 出口參數:無
- 備 注:
- ***********************************************************/
- void LCD_init(void)
- {
- LCD_RST = 0; // 產生一個讓LCD復位的低電平脈沖
- delay_1us();
- LCD_RST = 1;
- LCD_CE = 0; // 關閉LCD
- delay_1us();
- LCD_CE = 1; // 使能LCD
- delay_1us();
- LCD_write_byte(0x21, 0);// 使用擴展命令設置LCD模式
- LCD_write_byte(0xc8, 0);// 設置液晶偏置電壓
- LCD_write_byte(0x06, 0);// 溫度校正
- LCD_write_byte(0x13, 0);// 1:48
- LCD_write_byte(0x20, 0);// 使用基本命令,V=0,水平尋址
- LCD_clear(); // 清屏
- LCD_write_byte(0x0c, 0);// 設定顯示模式,正常顯示
-
- LCD_CE = 0; // 關閉LCD
- }
- /***********************************************************
- 函數名稱:LCD_set_XY
- 函數功能:設置LCD坐標函數
- 入口參數:X :0-83
- Y :0-5
- 出口參數:無
- 備 注:
- ***********************************************************/
- void LCD_set_XY(unsigned char X, unsigned char Y)
- {
- LCD_write_byte(0x40 | Y, 0); // column
- LCD_write_byte(0x80 | X, 0); // row
- }
- /***********************************************************
- 函數名稱:LCD_write_char
- 函數功能:顯示英文字符
- 入口參數:c: 顯示的字符
- 出口參數:無
- 備 注:
- ***********************************************************/
- void LCD_write_char(unsigned char c)
- {
- unsigned char line;
- c -= 32; //數組的行號
- for (line=0; line<6; line++)
- LCD_write_byte(font6x8[c][line], 1);
- }
- /***********************************************************
- 函數名稱:LCD_draw_bmp_pixel
- 函數功能:位圖繪制函數
- 入口參數:X、Y :位圖繪制的起始X、Y坐標;
- *map :位圖點陣數據;
- Pix_x :位圖像素(長)
- Pix_y :位圖像素(寬)
- 出口參數:無
- 備 注:
- ***********************************************************/
- void LCD_draw_bmp_pixel(unsigned char X,unsigned char Y,unsigned char *map, unsigned char Pix_x,unsigned char Pix_y)
- {
- unsigned int i,n;
- unsigned char row; //計算位圖所占行數
- if (Pix_y%8==0) //如果為位圖所占行數為整數
- row=Pix_y/8;
- else
- row=Pix_y/8+1;//如果為位圖所占行數不是整數
- LCD_set_XY(X,Y);
- for (n=0;n<row;n++) //換行
- {
- for(i=0;i<Pix_x;i++)
- {
- LCD_set_XY(X+i,Y+n);
- LCD_write_byte(map[i+n*Pix_x], 1);
- }
- }
- }
復制代碼
- #include <stdint.h>
- #include "msp430F5529.h"
- #define DOGS102x6_DRAW_NORMAL 0x00
- #define DOGS102x6_DRAW_INVERT 0x01
- // For all commands, CD signal must = 0
- #define SET_COLUMN_ADDRESS_MSB 0x10 //Set SRAM col. addr. before write, last 4 bits =
- // ca4-ca7
- #define SET_COLUMN_ADDRESS_LSB 0x00 //Set SRAM col. addr. before write, last 4 bits =
- // ca0-ca3
- #define SET_POWER_CONTROL 0x2F //Set Power control - booster, regulator, and follower
- // on
- #define SET_SCROLL_LINE 0x40 //Scroll image up by SL rows (SL = last 5 bits),
- // range:0-63
- #define SET_PAGE_ADDRESS 0xB0 //Set SRAM page addr (pa = last 4 bits), range:0-8
- #define SET_VLCD_RESISTOR_RATIO 0x27 //Set internal resistor ratio Rb/Ra to adjust contrast
- #define SET_ELECTRONIC_VOLUME_MSB 0x81 //Set Electronic Volume "PM" to adjust contrast
- #define SET_ELECTRONIC_VOLUME_LSB 0x0F //Set Electronic Volume "PM" to adjust contrast (PM =
- // last 5 bits)
- #define SET_ALL_PIXEL_ON 0xA4 //Disable all pixel on (last bit 1 to turn on all pixels
- // - does not affect memory)
- #define SET_INVERSE_DISPLAY 0xA6 //Inverse display off (last bit 1 to invert display -
- // does not affect memory)
- #define SET_DISPLAY_ENABLE 0xAF //Enable display (exit sleep mode & restore power)
- #define SET_SEG_DIRECTION 0xA1 //Mirror SEG (column) mapping (set bit0 to mirror
- // display)
- #define SET_COM_DIRECTION 0xC8 //Mirror COM (row) mapping (set bit3 to mirror display)
- #define SYSTEM_RESET 0xE2 //Reset the system. Control regs reset, memory not
- // affected
- #define NOP 0xE3 //No operation
- #define SET_LCD_BIAS_RATIO 0xA2 //Set voltage bias ratio (BR = bit0)
- #define SET_CURSOR_UPDATE_MODE 0xE0 //Column address will increment with write operation
- // (but no wrap around)
- #define RESET_CURSOR_UPDATE_MODE 0xEE //Return cursor to column address from before cursor
- // update mode was set
- #define SET_ADV_PROGRAM_CONTROL0_MSB 0xFA //Set temp. compensation curve to -0.11%/C
- #define SET_ADV_PROGRAM_CONTROL0_LSB 0x90
- int k;
- static const uint8_t FONT6x8[] = {
- /* 6x8 font, each line is a character each byte is a one pixel wide column
- * of that character. MSB is the top pixel of the column, LSB is the bottom
- * pixel of the column. 0 = pixel off. 1 = pixel on. */
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // space
- 0x00, 0x00, 0xFA, 0x00, 0x00, 0x00, // !
- 0x00, 0xE0, 0x00, 0xE0, 0x00, 0x00, // "
- 0x28, 0xFE, 0x28, 0xFE, 0x28, 0x00, // #
- 0x24, 0x54, 0xFE, 0x54, 0x48, 0x00, // $
- 0xC4, 0xC8, 0x10, 0x26, 0x46, 0x00, // %
- 0x6C, 0x92, 0x6A, 0x04, 0x0A, 0x00, // &
- 0x00, 0x10, 0xE0, 0xC0, 0x00, 0x00, // '
- 0x00, 0x38, 0x44, 0x82, 0x00, 0x00, // (
- 0x00, 0x82, 0x44, 0x38, 0x00, 0x00, // )
- 0x54, 0x38, 0xFE, 0x38, 0x54, 0x00, // *
- 0x10, 0x10, 0x7C, 0x10, 0x10, 0x00, // +
- 0x00, 0x02, 0x1C, 0x18, 0x00, 0x00, // ,
- 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, // -
- 0x00, 0x00, 0x06, 0x06, 0x00, 0x00, // .
- 0x04, 0x08, 0x10, 0x20, 0x40, 0x00, // /
- //96 Bytes
- 0x7C, 0x8A, 0x92, 0xA2, 0x7C, 0x00, // 0
- 0x00, 0x42, 0xFE, 0x02, 0x00, 0x00, // 1
- 0x42, 0x86, 0x8A, 0x92, 0x62, 0x00, // 2
- 0x84, 0x82, 0x92, 0xB2, 0xCC, 0x00, // 3
- 0x18, 0x28, 0x48, 0xFE, 0x08, 0x00, // 4
- 0xE4, 0xA2, 0xA2, 0xA2, 0x9C, 0x00, // 5
- 0x3C, 0x52, 0x92, 0x92, 0x0C, 0x00, // 6
- 0x82, 0x84, 0x88, 0x90, 0xE0, 0x00, // 7
- 0x6C, 0x92, 0x92, 0x92, 0x6C, 0x00, // 8
- 0x60, 0x92, 0x92, 0x94, 0x78, 0x00, // 9
- 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, // :
- 0x00, 0x00, 0x02, 0x2C, 0x00, 0x00, // ;
- 0x00, 0x10, 0x28, 0x44, 0x82, 0x00, // <
- 0x28, 0x28, 0x28, 0x28, 0x28, 0x00, // =
- 0x00, 0x82, 0x44, 0x28, 0x10, 0x00, // >
- 0x40, 0x80, 0x8A, 0x90, 0x60, 0x00, // ?
- //96*2 = 192 Bytes
- 0x7C, 0x82, 0xBA, 0x9A, 0x72, 0x00, // @
- 0x3E, 0x48, 0x88, 0x48, 0x3E, 0x00, // A
- 0xFE, 0x92, 0x92, 0x92, 0x6C, 0x00, // B
- 0x7C, 0x82, 0x82, 0x82, 0x44, 0x00, // C
- 0xFE, 0x82, 0x82, 0x82, 0x7C, 0x00, // D
- 0xFE, 0x92, 0x92, 0x92, 0x82, 0x00, // E
- 0xFE, 0x90, 0x90, 0x90, 0x80, 0x00, // F
- 0x7C, 0x82, 0x92, 0x92, 0x5E, 0x00, // G
- 0xFE, 0x10, 0x10, 0x10, 0xFE, 0x00, // H
- 0x00, 0x82, 0xFE, 0x82, 0x00, 0x00, // I
- 0x04, 0x02, 0x82, 0xFC, 0x80, 0x00, // J
- 0xFE, 0x10, 0x28, 0x44, 0x82, 0x00, // K
- 0xFE, 0x02, 0x02, 0x02, 0x02, 0x00, // L
- 0xFE, 0x40, 0x38, 0x40, 0xFE, 0x00, // M
- 0xFE, 0x20, 0x10, 0x08, 0xFE, 0x00, // N
- 0x7C, 0x82, 0x82, 0x82, 0x7C, 0x00, // O
- //96*3 = 288 Bytes
- 0xFE, 0x90, 0x90, 0x90, 0x60, 0x00, // P
- 0x7C, 0x82, 0x8A, 0x84, 0x7A, 0x00, // Q
- 0xFE, 0x90, 0x98, 0x94, 0x62, 0x00, // R
- 0x64, 0x92, 0x92, 0x92, 0x4C, 0x00, // S
- 0x80, 0x80, 0xFE, 0x80, 0x80, 0x00, // T
- 0xFC, 0x02, 0x02, 0x02, 0xFC, 0x00, // U
- 0xF8, 0x04, 0x02, 0x04, 0xF8, 0x00, // V
- 0xFC, 0x02, 0x1C, 0x02, 0xFC, 0x00, // W
- 0xC6, 0x28, 0x10, 0x28, 0xC6, 0x00, // X
- 0xC0, 0x20, 0x1E, 0x20, 0xC0, 0x00, // Y
- 0x86, 0x8A, 0x92, 0xA2, 0xC2, 0x00, // Z
- 0x00, 0xFE, 0x82, 0x82, 0x82, 0x00, // [
- 0x40, 0x20, 0x10, 0x08, 0x04, 0x00, // '\'
- 0x00, 0x82, 0x82, 0x82, 0xFE, 0x00, // ]
- 0x20, 0x40, 0x80, 0x40, 0x20, 0x00, // ^
- 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, // _
- //96*4 = 384 Bytes
- 0x00, 0xC0, 0xE0, 0x10, 0x00, 0x00, // `
- 0x04, 0x2A, 0x2A, 0x2A, 0x1E, 0x00, // a
- 0xFE, 0x14, 0x22, 0x22, 0x1C, 0x00, // b
- 0x1C, 0x22, 0x22, 0x22, 0x14, 0x00, // c
- 0x1C, 0x22, 0x22, 0x14, 0xFE, 0x00, // d
- 0x1C, 0x2A, 0x2A, 0x2A, 0x18, 0x00, // e
- 0x00, 0x10, 0x7E, 0x90, 0x40, 0x00, // f
- 0x18, 0x25, 0x25, 0x25, 0x3E, 0x00, // g
- 0xFE, 0x10, 0x20, 0x20, 0x1E, 0x00, // h
- 0x00, 0x22, 0xBE, 0x02, 0x00, 0x00, // i
- 0x00, 0x04, 0x02, 0x02, 0xBC, 0x00, // j
- 0x00, 0xFE, 0x08, 0x14, 0x22, 0x00, // k
- 0x00, 0x82, 0xFE, 0x02, 0x00, 0x00, // l
- 0x3E, 0x20, 0x1E, 0x20, 0x1E, 0x00, // m
- 0x3E, 0x10, 0x20, 0x20, 0x1E, 0x00, // n
- 0x1C, 0x22, 0x22, 0x22, 0x1C, 0x00, // o
- //96*5 = 480 Bytes
- 0x3F, 0x18, 0x24, 0x24, 0x18, 0x00, // p
- 0x18, 0x24, 0x24, 0x18, 0x3F, 0x00, // q
- 0x3E, 0x10, 0x20, 0x20, 0x10, 0x00, // r
- 0x12, 0x2A, 0x2A, 0x2A, 0x24, 0x00, // s
- 0x20, 0x20, 0xFC, 0x22, 0x24, 0x00, // t
- 0x3C, 0x02, 0x02, 0x04, 0x3E, 0x00, // u
- 0x38, 0x04, 0x02, 0x04, 0x38, 0x00, // v
- 0x3C, 0x02, 0x0C, 0x02, 0x3C, 0x00, // w
- 0x22, 0x14, 0x08, 0x14, 0x22, 0x00, // x
- 0x32, 0x09, 0x09, 0x09, 0x3E, 0x00, // y
- 0x22, 0x26, 0x2A, 0x32, 0x22, 0x00, // z
- 0x00, 0x10, 0x6C, 0x82, 0x00, 0x00, // {
- 0x00, 0x00, 0xEE, 0x00, 0x00, 0x00, // |
- 0x00, 0x82, 0x6C, 0x10, 0x00, 0x00, // }
- 0x40, 0x80, 0x40, 0x20, 0x40, 0x00, // ~
- 0x00, 0x60, 0x90, 0x90, 0x60, 0x00, // degrees symbol
- //96*6 = 576 Bytes
- //Special Characters ** NON-ASCII **
- 0x00, 0x00, 0xFE, 0x82, 0x82, 0x82, // [
- 0x82, 0x82, 0x82, 0xFE, 0x00, 0x00 // ]
- };
- uint8_t dogs102x6Memory[816 + 2];
- uint8_t currentPage = 0, currentColumn = 0;
- uint8_t backlight = 8;
- uint8_t contrast = 0x0F;
- uint8_t Dogs102x6_initMacro[] = {
- SET_SCROLL_LINE,
- SET_SEG_DIRECTION,
- SET_COM_DIRECTION,
- SET_ALL_PIXEL_ON,
- SET_INVERSE_DISPLAY,
- SET_LCD_BIAS_RATIO,
- SET_POWER_CONTROL,
- SET_VLCD_RESISTOR_RATIO,
- SET_ELECTRONIC_VOLUME_MSB,
- SET_ELECTRONIC_VOLUME_LSB,
- SET_ADV_PROGRAM_CONTROL0_MSB,
- SET_ADV_PROGRAM_CONTROL0_LSB,
- SET_DISPLAY_ENABLE,
- SET_PAGE_ADDRESS,
- SET_COLUMN_ADDRESS_MSB,
- SET_COLUMN_ADDRESS_LSB
- };
- /***********************************************************
- 函數名稱:delay xus
- 函數功能:模擬SPI接口時序寫數據/命令LCD
- 入口參數:us :寫入的數據;
- 出口參數:無
- 備 注:
- ***********************************************************/
- void delay_xus(unsigned xus)
- {
- while(xus--);
- }
- /***************************************************************************//**
- * @brief Sends commands to LCD via 3 wire SPI
- * @param sCmd Pointer to the commands to be written to the LCD
- * @param i Number of commands to be written to the LCD
- * @return None
- ******************************************************************************/
- void LCD_write_comd(uint8_t *scmd, uint8_t i)
- {
- uint16_t gie=__get_SR_register() & GIE;
- __disable_interrupt();
- P7OUT &=~BIT4; //CS=0,選中液晶
- P5OUT &=~BIT6; //輸入指令
- while(i)
- {
- while (!(UCB1IFG & UCTXIFG)) ;//等待發送
- UCB1TXBUF = *scmd; //send
- scmd++;
- i--;
- }
- while (UCB1STAT & UCBUSY) ;
- UCB1RXBUF;
- P7OUT |=BIT4;
- __bis_SR_register(gie);
- }
- /***************************************************************************//**
- * @brief Sends Data to LCD via 3 wire SPI
- * @param sData Pointer to the Data to be written to the LCD
- * @param i Number of data bytes to be written to the LCD
- * @return None
- ******************************************************************************/
- void LCD_write_data(uint8_t *sData, uint8_t i)
- {
- uint16_t gie=__get_SR_register() & GIE;
- __disable_interrupt();
- P7OUT &=~BIT4;
- P5OUT |=BIT6; //select input data
- while(i)
- {
- dogs102x6Memory[2 + (currentPage * 102) + currentColumn] = (uint8_t)*sData;
- currentColumn++;
- if (currentColumn > 101)
- {
- currentColumn = 101;
- }
- while (!(UCB1IFG & UCTXIFG)) ;
- UCB1TXBUF = *sData++;
- i--;
- }
- while (UCB1STAT & UCBUSY) ;
- UCB1RXBUF;
- P7OUT |= BIT4;
- __bis_SR_register(gie);
- }
- /***************************************************************************//**
- * @brief Sets Address of the LCD RAM memory
- *
- * (0,0) is the upper left corner of screen.
- * @param pa Page Address of the LCD RAM memory to be written (0 - 7)
- * @param ca Column Address of the LCD RAM memory to be written (0 - 101)
- * @return None
- ******************************************************************************/
- void Dogs102x6_setAddress(uint8_t pa, uint8_t ca)
- {
- uint8_t cmd[1];
- // Page boundary check
- if (pa > 7)
- {
- pa = 7;
- }
- // Column boundary check
- if (ca > 101)
- {
- ca = 101;
- }
- // Page Address Command = Page Address Initial Command + Page Address
- cmd[0] = SET_PAGE_ADDRESS + (7 - pa);
- uint8_t H = 0x00;
- uint8_t L = 0x00;
- uint8_t ColumnAddress[] = { SET_COLUMN_ADDRESS_MSB, SET_COLUMN_ADDRESS_LSB };
- currentPage = pa;
- currentColumn = ca;
- // Separate Command Address to low and high
- L = (ca & 0x0F);
- H = (ca & 0xF0);
- H = (H >> 4);
- // Column Address CommandLSB = Column Address Initial Command
- // + Column Address bits 0..3
- ColumnAddress[0] = SET_COLUMN_ADDRESS_LSB + L;
- // Column Address CommandMSB = Column Address Initial Command
- // + Column Address bits 4..7
- ColumnAddress[1] = SET_COLUMN_ADDRESS_MSB + H;
- // Set page address
- LCD_write_comd(cmd, 1);
- // Set column address
- LCD_write_comd(ColumnAddress, 2);
- }
- /***********************************************************
- 函數名稱:LCD_init
- 函數功能:5110初始化
- 入口參數:無
- 出口參數:無
- 備 注:
- ***********************************************************/
- void LCD_init(void)
- {
- P5DIR |=BIT7; //P57 out
- P5OUT &= ~BIT7; // P57=0
- P5OUT |= BIT7; //P57=1
- P7DIR |=BIT4; //P74 out
- P7OUT &=~BIT4; //P4=0
- P5DIR |=BIT6; //P56 out
- P5OUT &=BIT6; //P56=0
- P4SEL |=BIT1; //P41選擇 SIMO
- P4DIR |=BIT1; //P41 OUT
- P4SEL |=BIT3; //P43 select SCLK
- P4DIR |=BIT3; //SCLK out
- UCB1CTL1 |= UCSWRST;
- UCB1CTL0 = UCCKPH + UCMSB + UCMST + UCMODE_0 + UCSYNC;
- UCB1CTL1 = UCSSEL_2 + UCSWRST;
- UCB1BR0 = 0x02;
- UCB1BR1 = 0;
- UCB1CTL1 &= ~UCSWRST;
- UCB1IFG &= ~UCRXIFG;
- LCD_write_comd(Dogs102x6_initMacro, 13);
- P7OUT |=BIT4; //P74=1
- dogs102x6Memory[0] = 102;
- dogs102x6Memory[1] = 8;
- }
- /***************************************************************************//**
- * @brief Writes a character from FONT6x8[] array to the LCD at (row,col).
- *
- * (0,0) is the upper left corner of screen.
- * @param row Page Address (0 - 7) (there are 8 pages on the screen, each is 8 pixels tall)
- * @param col Column Address (there are 102 columns on the screen)
- * @param f Pointer to the character to be written to the LCD
- * @param style The style of the text
- * - NORMAL = 0 = dark letters with light background
- * - INVERT = 1 = light letters with dark background
- * @return None
- ******************************************************************************/
- void Dogs102x6_charDraw(uint8_t row, uint8_t col, uint16_t f, uint8_t style)
- {
- // Each Character consists of 6 Columns on 1 Page
- // Each Page presents 8 pixels vertically (top = MSB)
- uint8_t b;
- uint16_t h;
- uint8_t inverted_char[6];
- // Row boundary check
- if (row > 7)
- {
- row = 7;
- }
- // Column boundary check
- if (col > 101)
- {
- col = 101;
- }
- // handle characters not in our table
- if (f < 32 || f > 129)
- {
- // replace the invalid character with a '.'
- f = '.';
- }
- // subtract 32 because FONT6x8[0] is "space" which is ascii 32,
- // multiply by 6 because each character is columns wide
- h = (f - 32) * 6;
- Dogs102x6_setAddress(row, col);
- if (style == DOGS102x6_DRAW_NORMAL)
- {
- // write character
- LCD_write_data((uint8_t *)FONT6x8 + h, 6);
- }
- else
- {
- for (b = 0; b < 6; b++)
- {
- // invert the character
- inverted_char[b] = FONT6x8[h + b] ^ 0xFF;
- }
- // write inverted character
- LCD_write_data(inverted_char, 6);
- }
- }
- ////////////////////////////////////////////
- //CS--P7.4;DC--P5.6;SIMO--P4.1 //
- //EN--P7.6;RST--P5.7;SCLK--P4.3 //
- ////////////////////////////////////////////
- int main( void )
- { // Stop watchdog timer to prevent time out reset
- WDTCTL = WDTPW + WDTHOLD;
- P7DIR |=BIT4+BIT6;
- P5DIR |=BIT6+BIT7;
- P4DIR |=BIT1+BIT3;
- LCD_init();
- Dogs102x6_setAddress(25,4);
- Dogs102x6_stringDraw(0,0," AAAA ",DOGS102x6_DRAW_NORMAL);
- }
-
復制代碼
|