- /*******************************************************************************
- 文件名:mainSPI_oled12864_max7210-3.c
- 單片機型號: PICl6F877A
- 晶振頻率: 4MHz
- 功能: OLED 與使用MAX7219串行驅動8位共陰極LED數碼管同顯
- ********************************************************************************/
- //#include<pic.h>
- //#include<pic16f877a.h>
- #include<pic16f87x.h>
- #define uchar unsigned char
- #define uint unsigned int
- #define SPISCK_DIR TRISC3 //SPI時鐘SCK
- #define SPISCK RC3 //SPI時鐘SCK
- #define SPIMISO_DIR TRISC4 //SPI數據輸入SDI
- #define SPIMISO RC4 //SPI數據輸入SDI
- #define SPIMOSI_DIR TRISC5 //SPI數據輸出SDO
- #define SPIMOSI RC5 //SPI數據輸出SDO
- #define SPISS_DIR TRISC1 //SPI片選SS
- #define SPISS RC1 //SPI片選SS
- //#define OLED_RST_DIR TRISC2 //OLED復位
- //#define OLED_RST RC2 //OLED復位
- //#define OLED_SI SPIMOSI //RC5-串行數據輸入、輸出端
- //#define OLED_SCL SPISCK //RC3-同步時鐘輸入端
- //#define OLED_A0 SPIMISO //RC4-選擇命令還是數據
- //#define OLED_CS SPISS //RC1-片選
- #define RES_DIR TRISC2 //OLED復位
- #define RES RC2 //OLED復位
- #define OLED_SCL SPISCK //RC3-同步時鐘輸入端
- #define OLED_SI SPIMOSI //RC5-串行數據輸入、輸出端
- #define OLED_A0 SPIMISO //RC4-選擇命令還是數據
- #define OLED_CS1 SPISS //RC1-片選
- #define LCD_SS_DIR TRISC6 //LCD片選,輸出
- #define LCD_SS RC6 //LCD片選
- uchar LCD_code[10]={0x30,0x6d,0x5b,0x30,0x7F,0x5f,0x6d,0x79}; //12518623
- //{0x7e,0x30,0x6d,0x79,0x33,0x5b,0x5f,0x70,0x71,0x70b}; //表示不譯方式0~9
- //B碼方式數字0~9時,uchar tab[10]={0,1,2,3,4,5,6,7,8,9};
- void initiate_SPI(void) //SPI初始化程序
- {
- SSPCON=0x00;
- SSPSTAT=0x00; //清空配置寄存器
- SPISCK_DIR=0; //置SCK為輸出
- SPIMISO_DIR=1; //*** SDI為自動控制,本行可省略
- SPIMOSI_DIR=0; //置SDO為輸出
- SPISS_DIR=0; //OLED_SS為輸出
- SPISS=1; //OLED掛起
- RES_DIR=0; //置RST為輸出
- LCD_SS_DIR=0; //置LCD_SS為輸出
- LCD_SS=1; //LCD_SS掛起
- CKP=0; //時鐘閑置為低電平
- CKE=1; //為上升沿發送數據 *//
- SMP=0; //在輸出中間采樣輸入 *//
- SSPM3=0; SSPM2=0; //Fosc/16
- SSPM1=0; SSPM1=1;
- SSPIF=0;
- SSPIE= 0; //關中斷
- SSPEN=1; //SPI使能
- }
-
- void SPItransfer(uchar data) //SPI數據傳輸
- {
- WCOL=0; //清發送沖突標志位
- SSPIF=0; //清標志位
- SSPBUF=data; //發送數據
- while(!SSPIF); //等待發送完畢
- SSPIF=0; //*** 清標志位,本行可省略
- }
- //** 以下LCD專用
- void Max7219_write(uchar address,uchar data) //寫入MAX7219,LCD專用!!!
- {
- SPISS=1;
- LCD_SS=0; //LCD_SS選中
- SPItransfer(address); //寫人地址
- SPItransfer(data); //寫入數據
- LCD_SS=1;
- }
- void Max7219_init(void) //LCD專用!!!
- {
- Max7219_write(0x0c,0x01); //0x0e為關斷模式寄存器;0x01表示顯示器處于工作狀態
- Max7219_write(0x0a,0x0f); //0x0a為亮度調節寄存器;0x0f使數碼管顯示亮度為最亮
- Max7219_write(0x09,0x00);
- //0x09為譯碼模式選擇寄存器;0x00為非譯碼方式;0xf為選擇8位BCD-B譯碼模式
- Max7219_write(0x0b,0x07); //0x0b為掃描限制寄存器;0x07表示可接8個LED數碼管
- }
- void display(uchar * p) //數碼管8位顯示,LCD專用!!!
- {
- uchar i;
- for(i=0;i<8;i++)
- {
- Max7219_write(i+1,*(p+i));
- }
- }
- //** 以上LCD專用
- /*************************************************************************************************/
- /*--------------------------------------LCD功能定義----------------------------------------------*/
- /*************************************************************************************************/
- //uchar code ComTable[]={3,2,1,0,7,6,5,4,};
- uchar ComTable[]={3,2,1,0,7,6,5,4,};
- /*LCM顯示開關設定*/
- #define DISPLAY_ON() WriteCommand(0xaf) // Display on, 顯示開
- #define DISPLAY_OFF() WriteCommand(0xae) // Display off,顯示關
- /*LCM顯示ADC設定*/
- #define SET_ADC() WriteCommand(0xa1) // Reverse disrect (SEG131-SEG0,正向
- #define CLEAR_ADC() WriteCommand(0xa0) // Normal disrect (SEG0-SEG131),逆向
- /*LCM反顯設定*/
- #define REVERSE_DISPLAY_ON() WriteCommand(0xa7) // Reverse display : 0 illuminated,白字黑背景
- #define REVERSE_DISPLAY_OFF() WriteCommand(0xa6) // Normal display : 1 illuminated, 黑字白背景
- /*LCM顯示黑屏設定*/
- #define ENTIRE_DISPLAY_ON() WriteCommand(0xa5) // Entire dislay Force whole LCD point
- #define ENTIRE_DISPLAY_OFF() WriteCommand(0xa4) // Normal display
- /*LCM顯示偏壓設定 */
- #define SET_BIAS() WriteCommand(0xa3) // bias 1
- #define CLEAR_BIAS() WriteCommand(0xa2) // bias 0
- /*LCM */
- #define SET_MODIFY_READ() WriteCommand(0xe0) // Stop automatic increment of the column address by the read instruction
- #define RESET_MODIFY_READ() WriteCommand(0xee) // Cancel Modify_read, column address return to its initial value just before the Set Modify Read instruction is started
- /*LCM復位 */
- #define RESET() WriteCommand(0xe2)
- /*LCM顯示輸出模式選擇*/
- #define SET_SHL() WriteCommand(0xc8) // SHL 1,COM63-COM0 逆方向->從右到左顯示
- #define CLEAR_SHL() WriteCommand(0xc0) // SHL 0,COM0-COM63 順方向->從左到右顯示
- /*LCMLCM工作頻率選擇*/
- #define SET_OSC() WriteCommand(0xE4) //
- #define CLEAR_OSC() WriteCommand(0xE5) //
- /*************************************************************************************************/
- /*---------------------------------------8*16字模------------------------------------------------*/
- /*************************************************************************************************/
- //uchar rom font1[]=
- uchar font1[]=
- {
- /*-- 文字: 測 --*/
- /*-- 宋體12; 此字體下對應的點陣為:寬x高=16x16 --*/
- 0x08,0x31,0x86,0x60,0x00,0xFE,0x02,0xF2,0x02,0xFE,0x00,0xF8,0x00,0x00,0xFF,0x00,
- 0x04,0xFC,0x03,0x00,0x80,0x47,0x30,0x0F,0x10,0x67,0x00,0x07,0x40,0x80,0x7F,0x00,
- /*-- 文字: 試 --*/
- /*-- 宋體12; 此字體下對應的點陣為:寬x高=16x16 --*/
- 0x40,0x42,0xDC,0x08,0x00,0x90,0x90,0x90,0x90,0x90,0xFF,0x10,0x12,0x1C,0x10,0x00,
- 0x00,0x00,0x7F,0x20,0x10,0x20,0x20,0x1F,0x10,0x10,0x01,0x06,0x18,0x20,0x78,0x00,};
- /*-- 文字: 測 --*/
- /*-- 宋體12; 此字體下對應的點陣為:寬x高=16x16 --*/
- //0x08,0x31,0x86,0x60,0x00,0xFE,0x02,0xF2,0x02,0xFE,0x00,0xF8,0x00,0x00,0xFF,0x00,
- //0x04,0xFC,0x03,0x00,0x80,0x47,0x30,0x0F,0x10,0x67,0x00,0x07,0x40,0x80,0x7F,0x00,
- /*-- 文字: 試 --*/
- /*-- 宋體12; 此字體下對應的點陣為:寬x高=16x16 --*/
- //0x40,0x42,0xDC,0x08,0x00,0x90,0x90,0x90,0x90,0x90,0xFF,0x10,0x12,0x1C,0x10,0x00,
- //0x00,0x00,0x7F,0x20,0x10,0x20,0x20,0x1F,0x10,0x10,0x01,0x06,0x18,0x20,0x78,0x00,
- /*-- 文字: 測 --*/
- /*-- 宋體12; 此字體下對應的點陣為:寬x高=16x16 --*/
- //0x08,0x31,0x86,0x60,0x00,0xFE,0x02,0xF2,0x02,0xFE,0x00,0xF8,0x00,0x00,0xFF,0x00,
- //0x04,0xFC,0x03,0x00,0x80,0x47,0x30,0x0F,0x10,0x67,0x00,0x07,0x40,0x80,0x7F,0x00,
- /*-- 文字: 試 --*/
- /*-- 宋體12; 此字體下對應的點陣為:寬x高=16x16 --*/
- //0x40,0x42,0xDC,0x08,0x00,0x90,0x90,0x90,0x90,0x90,0xFF,0x10,0x12,0x1C,0x10,0x00,
- //0x00,0x00,0x7F,0x20,0x10,0x20,0x20,0x1F,0x10,0x10,0x01,0x06,0x18,0x20,0x78,0x00,
- //};
- /*************************************************************************************************/
- /*--------------------------------------LCM寫數據函數--------------------------------------------*/
- /*************************************************************************************************/
- void Write_Data(uchar dat)
- {
- uchar i;
- uchar z;
- OLED_CS1= 0;
- OLED_A0 = 1;
- for(i=0;i<8;i++)
- {
- OLED_SCL = 0;
- //OLED_SI = dat && 0x80;//取最高位
- OLED_SI=(dat&0x80)==0?0:1;
- z=OLED_SI;
- dat <<= 1; //左移
- OLED_SCL = 1;
- }
- OLED_CS1= 1;
- }
- /*************************************************************************************************/
- /*------------------------------------LCM寫指令函數----------------------------------------------*/
- /*************************************************************************************************/
- void WriteCommand(uchar cmd)
- {
- uchar i;
- OLED_SCL = 1;
- OLED_SCL = 0;
- OLED_CS1= 0;
- OLED_A0 = 0;
- for(i=0;i<8;i++)
- {
- OLED_SI = cmd && 0x80;//取最高位
- OLED_SCL = 1;
- OLED_SCL = 0;
- cmd <<= 1;//左移
- }
- OLED_CS1= 1;
- }
- /*************************************************************************************************/
- /*--------------------------------------LCM頁基址設置--------------------------------------------*/
- /*************************************************************************************************/
- void Set_Page_Address(uchar add)
- {
- add=0xb0|add;
- WriteCommand(add);
- }
- /*************************************************************************************************/
- /*--------------------------------------LCM寫數據函數--------------------------------------------*/
- /*************************************************************************************************/
- void Set_Column_Address(uchar add)
- {
- WriteCommand((0x10|(add>>4)));
- WriteCommand((0x0f&add));
- return;
- }
- /*************************************************************************************************/
- /*----------------------------------------LCM清屏函數--------------------------------------------*/
- /*************************************************************************************************/
- void LcmClear( void )
- {
- uchar i,j;
- WriteCommand(0x40); //Set Display Start Line = com0
- for(i=0;i<8;i++)
- {
- WriteCommand(0xB0|ComTable[i]);
- WriteCommand(0x10); //Set Column Address = 0
- WriteCommand(0x01); //Colum from 1 -> 129 auto add
- for(j=0;j<132;j++)
- {
- Write_Data( 0 ); //寫入0
- }
- }
- }
- /**************************************************************************************************/
- /*----------------------------------------延時函數------------------------------------------------*/
- /**************************************************************************************************/
- void Delay(uint n)
- {
- while(n--);
- }
- /**************************************************************************************************/
- /*----------------------------------------LCM電源控制項設定函數-----------------------------------*/
- /**************************************************************************************************/
- void Power_Control(uchar vol)
- {
- WriteCommand((0x28|vol));
- }
- /**************************************************************************************************/
- /*----------------------------------------LCM偏置電壓函數-----------------------------------------*/
- /**************************************************************************************************/
- void Regulor_Resistor_Select(uchar r)
- {
- WriteCommand((0x20|r));
- }
- /**************************************************************************************************/
- /*----------------------------------------LCM電量設置函數-----------------------------------------*/
- /**************************************************************************************************/
- void Set_Contrast_Control_Register(uchar mod)
- {
- WriteCommand(0x81);
- WriteCommand(mod);
- }
- /**************************************************************************************************/
- /*----------------------------------------顯示起始行定義函數--------------------------------------*/
- /**************************************************************************************************/
- void Initial_Dispay_Line(uchar line)
- {
- line|=0x40;
- WriteCommand(line);
- return;
- }
- /**************************************************************************************************/
- /*----------------------------------------LCM初始化函數-------------------------------------------*/
- /**************************************************************************************************/
- void LcmInitial( void )
- {
- RESET();
- DISPLAY_OFF();
- LcmClear();
- Delay(1000);
- OLED_CS1=1;
- SET_ADC(); /*LCM顯示ADC設定,順方向 */
- CLEAR_SHL(); /*LCM公共輸出模式選擇,順方向 */
- SET_BIAS(); /*LCM顯示偏壓設定(取中間值) */
- Power_Control(0x07); /*LCM電源控制項設定,這里全開 */
- Regulor_Resistor_Select(0x04); /*調整V0電阻比率,這里折中*/
- Set_Contrast_Control_Register(0x28); //NT7534綠0x28//ST7565藍0x0a
- Initial_Dispay_Line(0x00); /*開顯示*/
- DISPLAY_ON();
- }
- /**************************************************************************************************/
- /*--------------------------------------LCM漢字顯示函數-------------------------------------------*/
- /**************************************************************************************************/
- void Display_Chinese(uchar ft[],uchar x,uchar y) //字庫數組,起始行,起始列
- {
- uchar i,j,k,num=0;
- i=x;
- for(i;i<x+2;i++) //行
- {
- num=i<<7;
- num=num>>3;
- Set_Page_Address(i);
- Set_Column_Address(y);//列地址
- for(j=0;j<0x02;j++) //行字數
- {
- for(k=0;k<0x10;k++)
- {
- Write_Data(ft[num+k]);
- }
- num+=0x20;
- }
- }
- }
- //------------------------------------------------------------------------------
- int main(void)
- {
- //dat=0x55;
- Write_Data(0x55);
- uchar i=0;
- initiate_SPI(); //SPI初始化
- Max7219_init(); //LCD初始化
- LcmInitial(); //OLED初始化
- while(1)
- {
- LcmClear(); //OLED清屏
- Delay(100);
- DISPLAY_ON();
- REVERSE_DISPLAY_OFF();
- Display_Chinese(font1,4,0); //OLED顯示
- display(LCD_code); //LCD顯示
- }
- }
復制代碼 |