找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 7407|回復(fù): 2
打印 上一主題 下一主題
收起左側(cè)

有oled就有bad apple 單片機(jī)驅(qū)動(dòng)OLED12864播放動(dòng)畫程序

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:394949 發(fā)表于 2019-7-14 16:15 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
單片機(jī)源程序如下:
  1. /**********************************************************************************************
  2. 程序名:STC單片機(jī)驅(qū)動(dòng)OLED12864播放動(dòng)畫程序
  3. 編寫人:黑色蒲公英
  4. 編寫時(shí)間:2019年1月22日
  5. 硬件支持:STC12C5A60S2單片機(jī),7針SPI通信方式OLED12864顯示屏,128M內(nèi)存卡(也可使用2G以下的),TF卡座模塊(5V供電,帶電平轉(zhuǎn)換芯片)
  6. 備注:本程序是本人整理修改而來(lái)共享給大家,希望大家分享時(shí)保留原作者(黑色蒲公英)的網(wǎng)名。
  7.           播放視頻效果可在快手搜索x手號(hào)碼(昵稱@黑色蒲公英)查看,如需制作可
  8.           在我的快手小店買到制作所需要的所有配件,如需指導(dǎo)可添加QQ群:7297 33027,歡迎你加入
  9.           STC單片機(jī)學(xué)習(xí)交流群,一起學(xué)習(xí)交流電子技術(shù)。
  10. **********************************************************************************************/
  11. //Modle :
  12. //Driver IC:SSD1306
  13. //Interfaces:SPI-4
  14. #include<reg51.h>
  15. //以下是OLED12864引腳接線定義(根據(jù)自己的實(shí)際接線可修改)
  16. sbit _CS=P3^4;
  17. sbit RES=P3^3;
  18. sbit RS=P3^2;
  19. sbit _SCL=P3^0;/*serial clock input(D6)*/
  20. sbit _SI=P3^1;/*serial data input(D7)*/

  21. //以下是內(nèi)存卡接線定義(根據(jù)自己的實(shí)際接線可修改)
  22. sbit SI=P1^1;
  23. sbit SO=P1^0;
  24. sbit CS=P1^3;
  25. sbit SCL=P1^2;/*serial clock input(D6)*/

  26. #define uchar unsigned char
  27. #define uint unsigned int
  28. #define Page0    0xb0  //第0頁(yè)地址
  29. #define Page_max   0xb8  //最大頁(yè)地址,第8頁(yè)

  30. #define Column_h   0x10  //列地址(高位)
  31. #define Column_l   0x00  //列地址(低位)

  32. //錯(cuò)誤碼定義
  33. #define INIT_CMD0_ERROR 0x01
  34. #define INIT_CMD1_ERROR 0x02
  35. #define WRITE_BLOCK_ERROR 0x03
  36. #define READ_BLOCK_ERROR 0x04
  37. //變量定義
  38. unsigned char is_init;//初始化標(biāo)志位
  39. unsigned char xdata pbuf[512];//數(shù)據(jù)緩沖區(qū)
  40. typedef signed char s8;
  41. typedef signed int  s16;
  42. typedef signed long s32;

  43. typedef unsigned char u8;
  44. typedef unsigned int  u16;
  45. typedef unsigned long u32;


  46. //延時(shí)函數(shù)
  47. void delay(unsigned int time){
  48.         while(time--);
  49. }
  50. //SPI寫入一個(gè)字節(jié)
  51. void spi_write(unsigned char x){
  52.                                 unsigned char i;
  53.         for(i=0;i<8;i++){
  54.                 SI = ((x<<i)&0x80);
  55.                 SCL = 0;
  56.                 if(is_init)delay(8);//is_init為1時(shí),初始化時(shí)延時(shí)以降低時(shí)鐘
  57.                 SCL = 1;
  58.                 if(is_init)delay(8);//is_init為1時(shí),初始化時(shí)延時(shí)以降低時(shí)鐘
  59.         }
  60. }
  61. //SPI讀取一個(gè)字節(jié)
  62. unsigned char spi_read(){
  63.                         unsigned char temp = 0,i;
  64.                 SO = 1;
  65.                 for(i=0;i<8;i++){
  66.                 SCL = 0;
  67.                 if(is_init)delay(8);//is_init為1時(shí),初始化時(shí)延時(shí)以降低時(shí)鐘
  68.                 if(SO)temp|=(0x80>>i);
  69.                 SCL = 1;
  70.                 if(is_init)delay(8);//is_init為1時(shí),初始化時(shí)延時(shí)以降低時(shí)鐘
  71.                 }
  72.                 return (temp);
  73. }
  74. //向SD卡寫命令
  75. unsigned char Write_Cmd(unsigned char *pcmd){
  76.                                                 unsigned char temp,time = 0,i;
  77.                 for(i=0;i<6;i++){//發(fā)送6個(gè)字節(jié)命令字節(jié)序列
  78.                 spi_write(pcmd[i]);
  79.                 }
  80.                 do{
  81.                         temp = spi_read();//一直讀,直到讀到不是0xff或超時(shí)
  82.                         time++;
  83.                 }
  84.                 while((temp==0xff)&&(time<100));
  85.                 return(temp);
  86. }
  87. //SD卡復(fù)位函數(shù) 使用CMD0命令
  88. unsigned char SD_Reset(){
  89.                                 unsigned char time,temp,i;
  90.                                 unsigned char pcmd[]={//CMD0命令
  91.                                 0x40,0x00,0x00,0x00,0x00,0x95
  92.                                 };
  93.                 is_init = 1;//set the init flag
  94.                 CS = 1;
  95.                 for(i=0;i<0x0f;i++){//初始化時(shí)至少發(fā)送74個(gè)時(shí)鐘信號(hào),這是必須的
  96.                 spi_write(0xff);//0x0f*0xff=15*8=120CLK
  97.                 }
  98.                 CS = 0;
  99.                 time = 0;
  100.                 do{
  101.                         temp = Write_Cmd(pcmd);//寫入CMD0命令
  102.                         time++;
  103.                 if(time==200){
  104.                         return(INIT_CMD0_ERROR);//CMD0寫入失敗
  105.         }
  106. }
  107.                 while(temp!=0x01);
  108.                 CS = 1;
  109.                 spi_write(0xff);//補(bǔ)8個(gè)CLK(SD卡時(shí)序要求)
  110.                 return 0;//返回0說(shuō)明復(fù)位成功
  111. }
  112. //SD卡初始化函數(shù) 使用CMD1命令
  113. unsigned char SD_Init(){
  114.                                 unsigned char temp,time;
  115.                                 unsigned char pcmd[]={//CMD1命令
  116.                                 0x41,0x00,0x00,0x00,0x00,0xff
  117.                                 };
  118.                 CS = 0;
  119.                 time = 0;
  120.                 do{
  121.                         temp = Write_Cmd(pcmd);
  122.                         time++;
  123.                 if(time==100){
  124.                         return(INIT_CMD1_ERROR);//CMD1命令寫入失敗
  125.         }
  126. }
  127.                 while(temp!=0);
  128.                 is_init = 0;//初始化完畢,將is_init置0,提高數(shù)據(jù)傳輸速度
  129.                 CS = 1;//關(guān)SD卡片選
  130.                 spi_write(0xff);//補(bǔ)8個(gè)CLK
  131.                 return (0);//返回0說(shuō)明初始化成功
  132. }

  133. //讀SD卡扇區(qū) 使用CMD17命令
  134. unsigned char SD_read_sector(unsigned long addr,unsigned char *buffer){
  135.                         unsigned int i;
  136.                         unsigned char temp,time;
  137.                         unsigned char pcmd[]={//CMD17命令
  138.                         0x51,0x00,0x00,0x00,0x00,0xff
  139.                         };
  140.                 addr <<= 9;//addr= addr*512;將塊地址(扇區(qū)地址)轉(zhuǎn)換為字節(jié)地址
  141.                 pcmd[1]=((addr&0xff000000)>>24);
  142.                 pcmd[2]=((addr&0x00ff0000)>>16);
  143.                 pcmd[3]=((addr&0x0000ff00)>>8);
  144.                 CS = 0;//開(kāi)SD卡片選
  145.                 time = 0;
  146.                 do{
  147.                         temp = Write_Cmd(pcmd);//寫入CMD17命令
  148.                         time++;
  149.                         if(time==100){
  150.                                 return(READ_BLOCK_ERROR);//讀塊失敗
  151.                         }
  152.                 }
  153.                 while(temp!=0);
  154.                 while(spi_read()!=0xfe);//一直讀,當(dāng)讀到0xfe時(shí),說(shuō)明后面是512字節(jié)數(shù)據(jù)
  155.                 for(i=0;i<512;i++){
  156.                         buffer[i] = spi_read();//將數(shù)據(jù)寫入到緩沖區(qū)中
  157.                 }
  158.                         spi_read();//讀兩個(gè)字節(jié)CRC校驗(yàn)碼
  159.                         spi_read();
  160.                         CS = 1;//關(guān)SD卡片選
  161.                         spi_write(0xff);//補(bǔ)8個(gè)CLK
  162.                         return 0;
  163. }
  164. void Delayt(unsigned int time)
  165.         {
  166.                   unsigned int i,j;
  167.                   for(i=0;i<time;i++)
  168.                      for(j=0;j<20000;j++)
  169.                                {;}
  170.                 }
  171. void Ins_trans(unsigned char command) //寫命令函數(shù)
  172.   {
  173.     unsigned char counter;
  174.           _CS=0;
  175.           RS=0;
  176.     for(counter=0;counter<8;counter++)
  177.        {
  178.              _SCL=0;
  179.          _SI=(command&0x80)>>7;
  180.          command=command<<1;
  181.                      _SCL=1;
  182.                      _SCL=0;
  183.         }
  184.         RS=1;
  185.    _CS=1;
  186.   }
  187. void Data_trans(unsigned char command)//寫數(shù)據(jù)函數(shù)
  188.   {
  189.     unsigned char counter;
  190.           _CS=0;
  191.           RS=1;
  192.     for(counter=0;counter<8;counter++)
  193.        {
  194.          _SCL=0;
  195.              _SI=(command&0x80)>>7;
  196.                  command=command<<1;   
  197.                      _SCL=1;
  198.                      _SCL=0;
  199.        }
  200.         RS=1;
  201.    _CS=1;
  202.    }
  203. void Column_set(unsigned char column)
  204.           {
  205.                      Ins_trans(0x10|(column>>4));
  206.                      Ins_trans(0x02|(column&0x0f));         
  207.                   }
  208. void Page_set(unsigned char page)
  209.           {
  210.                     Ins_trans(0xb0+page);
  211.                   }

  212. void lcdinit(){ //SSD1306初始化函數(shù)
  213.         RES=1;
  214.   Delayt(10);
  215.   RES=0;
  216.   Delayt(10);
  217.   RES=1;
  218.   Delayt(10);
  219. Ins_trans(0xAE); /*display off*/

  220. Ins_trans(0x00); /*set lower column address*/
  221. Ins_trans(0x10); /*set higher column address*/
  222. Ins_trans(0x40); /*set display start line*/

  223. Ins_trans(0xB0); /*set page address*/

  224. Ins_trans(0x81); /*contract control*/
  225. Ins_trans(0x66); /*128*/

  226. Ins_trans(0xA1); /*set segment remap*/

  227. Ins_trans(0xA6); /*normal / reverse*/

  228. Ins_trans(0xA8); /*multiplex ratio*/
  229. Ins_trans(0x3F); /*duty = 1/64*/

  230. Ins_trans(0xC8); /*Com scan direction*/

  231. Ins_trans(0xD3); /*set display offset*/
  232. Ins_trans(0x00);
  233. Ins_trans(0xD5); /*set osc division*/
  234. Ins_trans(0x80);
  235. Ins_trans(0xD9); /*set pre-charge period*/
  236. Ins_trans(0x1f);

  237. Ins_trans(0xDA); /*set COM pins*/
  238. Ins_trans(0x12);

  239. Ins_trans(0xdb); /*set vcomh*/
  240. Ins_trans(0x30);
  241. Ins_trans(0x8d); /*set charge pump enable*/
  242. Ins_trans(0x14);

  243. Ins_trans(0xAF); /*display ON*/
  244.   }

  245. void Picture_display_on(unsigned char *ptr_pic)//顯示上半屏
  246.         {
  247.                   unsigned char page,column;
  248.                   for(page=0;page<4;page++)        //page loop
  249.                      {
  250.                            Page_set(page);
  251.                            Column_set(2);          
  252.                            for(column=2;column<130;column++)        //column loop
  253.                               {
  254.                                      Data_trans(*ptr_pic++);
  255.                                   }
  256.                          }
  257.                 }
  258. void Picture_display_down(unsigned char *ptr_pic)//顯示下半屏
  259.         {
  260.                   unsigned char page,column;
  261.                   for(page=4;page<8;page++)        //page loop
  262.                      {
  263.                            Page_set(page);
  264.                            Column_set(2);          
  265.                            for(column=2;column<130;column++)        //column loop
  266.                               {
  267.                                      Data_trans(*ptr_pic++);
  268.                                   }
  269.                          }
  270.                 }

  271. void main(void)
  272.      {
  273.          unsigned int i;
  274.                 SD_Reset();//復(fù)位SD卡,進(jìn)入SPI模式
  275.                 SD_Init();//初始化SD卡
  276.                 lcdinit();//OLED12864初始化
  277.                
  278.         while(1)
  279.         {
  280.                  for(i=0;i<14706;i+=2){
  281.                 SD_read_sector(8195+i,pbuf);//從SD卡8193扇區(qū)讀取512字節(jié)數(shù)據(jù)(扇區(qū)號(hào)用winhex打開(kāi)內(nèi)存卡查看,把你看到的扇區(qū)號(hào)替換掉8193即可)
  282.                 Picture_display_down(&pbuf); //打印下半屏
  283.                 Ins_trans(0xA7); /*normal / reverse*///A7反色顯示,A6正常顯示
  284.                 SD_read_sector(8195+i+1,pbuf);//從SD卡8194扇區(qū)讀取512字節(jié)數(shù)據(jù)(扇區(qū)號(hào)用winhex打開(kāi)內(nèi)存卡查看,把你看到的扇區(qū)號(hào)替換掉8193即可)
  285.                 Picture_display_on(&pbuf);//打印上半屏  
  286.         }
  287.         }
  288. }
  289.          
復(fù)制代碼

所有程序51hei提供下載:
1.rar (37.23 KB, 下載次數(shù): 72)


評(píng)分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評(píng)分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏3 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:733209 發(fā)表于 2020-5-22 15:01 來(lái)自手機(jī) | 只看該作者
SD卡里面是啥數(shù)據(jù)。二進(jìn)制。ascll。文本?還是啥文件
回復(fù)

使用道具 舉報(bào)

板凳
ID:733209 發(fā)表于 2020-5-22 15:01 來(lái)自手機(jī) | 只看該作者
SD卡里面是啥數(shù)據(jù)。二進(jìn)制。ascll。文本?還是啥文件
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表