找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2360|回复: 0
收起左侧

stm32 9328彩色26万色液晶驱动

[复制链接]
ID:82781 发表于 2015-6-14 01:30 | 显示全部楼层 |阅读模式
  1. #include "stm32f10x.h"
  2. #define SET_CS GPIOD->BSRR=0x00001000
  3. #define CLR_CS GPIOD->BRR=0x00001000
  4. #define SET_WR GPIOD->BSRR=0x00004000
  5. #define CLR_WR GPIOD->BRR=0x00004000
  6. #define SET_RD GPIOD->BSRR=0x00008000
  7. #define CLR_RD GPIOD->BRR=0x00008000
  8. #define SET_RS GPIOD->BSRR=0x00002000
  9. #define CLR_RS GPIOD->BRR=0x00002000

  10. GPIO_InitTypeDef GPIO_InitStructure;
  11. void mysysinit(void);//系统时钟初始
  12. void my_USART_init(void);//初始化
  13. void my_send_byte(unsigned char send_date); //发送一个字节
  14. void delay1us(uint32_t);//延时1微秒程序
  15. void init_STM32(void); //开机初始化
  16. uint16_t read_LCD_register(uint16_t reg);//读出指定的液晶寄存器,
  17. void wr_cmd_date(uint16_t reg,uint16_t date); //写入指定的寄存器值
  18. void LCD9328_init(void);//9328初始化
  19. void lcm_coordinate(uint16_t x1,uint16_t y1 );//扫描地
  20. int main(void)
  21. {
  22. uint32_t numb;
  23.       uint16_t  x,y,buff_sd_tft;
  24.    init_STM32();///开机初始化时钟,初始化串口
  25.   // delay1us(20000);
  26.   LCD9328_init();
  27. // delay1us(10000);
  28.    buff_sd_tft=0x0000;
  29.    y=0;x=0;//lcm_coordinate(x,y);
  30.   while(1)
  31.   {

  32.    
  33.         
  34.     //lcm_coordinate(x,y);
  35.          wr_cmd_date(0x22,buff_sd_tft);
  36.          
  37.          //delay1us(200);                     
  38.       numb++;
  39.     if(numb==76800){numb=0;buff_sd_tft=(buff_sd_tft+215);}
  40.    

  41.   }


  42. /* d=read_LCD_register(0x20); //读液晶的00寄存器,判断是9525(9328)|9320
  43.     e=d;
  44.    a=e;
  45.    c=(d>>8);
  46.    my_send_byte(c);
  47.    my_send_byte(a);
  48.     while(1);*/




  49. }
  50. void lcm_coordinate(uint16_t x1,uint16_t y1 )//扫描地
  51. {

  52.   wr_cmd_date(0x0050,x1);
  53.   wr_cmd_date(0x0051,x1);
  54.   wr_cmd_date(0x0052,y1);
  55.   wr_cmd_date(0x0053,y1);
  56.   wr_cmd_date(0x0020,x1);
  57.   wr_cmd_date(0x0021,y1);

  58. }
  59. void LCD9328_init()//横屏初始
  60. {

  61. delay1us(150000);
  62.   wr_cmd_date(0x0001,0x0100); //s1-s720
  63.   wr_cmd_date(0x0002,0x0700);
  64.   wr_cmd_date(0x0003,0x1030);
  65. wr_cmd_date(0x0004,0x0000);
  66.   wr_cmd_date(0x0008,0x0207);
  67.   wr_cmd_date(0x0009,0x0000);
  68.   wr_cmd_date(0x000A,0x0000);
  69.   wr_cmd_date(0x000C,0x0000);
  70.   wr_cmd_date(0x000D,0x0000);
  71. wr_cmd_date(0x000F,0x0000);
  72. //power on sequence VGHVGL
  73.   wr_cmd_date(0x0010,0x0000);  
  74.   wr_cmd_date(0x0011,0x0007);
  75.   wr_cmd_date(0x0012,0x0000);
  76.   wr_cmd_date(0x0013,0x0000);
  77. //vgh
  78.   wr_cmd_date(0x0010,0x1290);  
  79.   wr_cmd_date(0x0011,0x0227);
  80. delay1us(800000);
  81. //vregiout
  82.   wr_cmd_date(0x0012,0x001d); //0x001b
  83. delay1us(800000);
  84. //vom amplitude
  85.   wr_cmd_date(0x0013,0x1500);
  86. delay1us(800000);
  87. //vom H
  88.   wr_cmd_date(0x0029,0x0018);
  89.   wr_cmd_date(0x002B,0x000D);

  90. //gamma
  91.   wr_cmd_date(0x0030,0x0004);
  92.   wr_cmd_date(0x0031,0x0307);
  93.   wr_cmd_date(0x0032,0x0002);// 0006
  94.   wr_cmd_date(0x0035,0x0206);
  95.   wr_cmd_date(0x0036,0x0408);
  96.   wr_cmd_date(0x0037,0x0507);
  97.   wr_cmd_date(0x0038,0x0204);//0200
  98.   wr_cmd_date(0x0039,0x0707);
  99.   wr_cmd_date(0x003C,0x0405);// 0504
  100.   wr_cmd_date(0x003D,0x0F02);
  101. //ram
  102.   wr_cmd_date(0x0050,0x0000);
  103.   wr_cmd_date(0x0051,0x00EF);
  104.   wr_cmd_date(0x0052,0x0000);
  105.   wr_cmd_date(0x0053,0x013F);
  106.   wr_cmd_date(0x0060,0xA700);
  107.   wr_cmd_date(0x0061,0x0001);
  108.   wr_cmd_date(0x006A,0x0000);
  109. //
  110.   wr_cmd_date(0x0080,0x0000);
  111.   wr_cmd_date(0x0081,0x0000);
  112.   wr_cmd_date(0x0082,0x0000);
  113.   wr_cmd_date(0x0083,0x0000);
  114.   wr_cmd_date(0x0084,0x0000);
  115.   wr_cmd_date(0x0085,0x0000);
  116. //
  117.   wr_cmd_date(0x0090,0x0010);
  118.   wr_cmd_date(0x0093,0x0003);
  119.   wr_cmd_date(0x0095,0x0110);
  120.   wr_cmd_date(0x0097,0x0000);
  121.   wr_cmd_date(0x0098,0x0000);
  122.   wr_cmd_date(0x0007,0x0133);
  123. }
  124. void wr_cmd_date(uint16_t reg,uint16_t date)
  125. {

  126.   GPIOE->CRL=0x33333333; //数据口转换成输出
  127.   GPIOE->CRH=0x33333333; //数据口转换成输出

  128.   CLR_CS;
  129.   CLR_RS;
  130.   SET_RD;
  131.   CLR_WR;
  132.   GPIOE->ODR=reg;//GPIO_Write(GPIOE, reg);// //写入命令
  133.   SET_WR;
  134.   SET_RS;
  135.   //delay_1us(1);
  136.   CLR_WR;
  137. GPIOE->ODR=date;//GPIO_Write(GPIOE, date); // //写入命令
  138.   SET_WR;
  139.   SET_CS;

  140. }
  141. uint16_t read_LCD_register(uint16_t reg)
  142. {
  143.    uint16_t date;
  144. GPIOE->CRL=0x33333333; //数据口转换成输出
  145. GPIOE->CRH=0x33333333; //数据口转换成输出
  146.    CLR_CS;
  147.    //delay_1us(1);
  148.    CLR_RS;
  149.    //delay_1us(1);
  150.    SET_RD;
  151.    CLR_WR;
  152.    GPIO_Write(GPIOE, reg); //写入命令
  153.    SET_WR;
  154.    //delay_1us(1);
  155.    CLR_RD;
  156.    GPIOE->CRL=0x88888888; //数据口转换成输入
  157.    GPIOE->CRH=0x88888888; //数据口转换成输入
  158.   // for(n=200;n;n--);
  159.    date=(GPIOE->IDR);//读出数据
  160.    SET_RD;
  161.    SET_CS;
  162.   

  163.   return (date);


  164. }
  165. void init_STM32()
  166. {
  167.   mysysinit();//RCC初始化,系统时钟设置72MHZ
  168.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//使能APB2的GPIO_A时钟
  169.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);//使能APB2的GPIO_D时钟
  170.    // RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);//使能APB2的GPIO_B时钟
  171. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE);//使能APB2的GPIO_E时钟
  172. //RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);//使能APB2的GPIO_E时钟
  173.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);//使能APB1的USART2时钟
  174. /* 设置PD口用于控制LCD的为输出 */
  175.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13| GPIO_Pin_14| GPIO_Pin_15| GPIO_Pin_8|
  176.                                  GPIO_Pin_9 | GPIO_Pin_10| GPIO_Pin_11;
  177.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  178.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  179.   GPIO_Init(GPIOD, &GPIO_InitStructure);
  180. /* 设置PE口用于控制LCD的为输出 */
  181.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1| GPIO_Pin_2| GPIO_Pin_3| GPIO_Pin_4|
  182.                                 GPIO_Pin_5 | GPIO_Pin_6| GPIO_Pin_7| GPIO_Pin_8| GPIO_Pin_9|
  183.                                 GPIO_Pin_10 | GPIO_Pin_11| GPIO_Pin_12| GPIO_Pin_13| GPIO_Pin_14|
  184.                                 GPIO_Pin_15 ;
  185.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  186.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  187.   GPIO_Init(GPIOD, &GPIO_InitStructure);
  188.   my_USART_init();

  189.              my_send_byte(0);
  190.           my_send_byte(1);
  191.        my_send_byte(2);
  192.     my_send_byte(3);

  193. }
  194. void delay1us(uint32_t time)
  195. {   uint32_t b,c;
  196.         for(c=time;c;c--) //定时=time*3*333.6=time*1000.8ns
  197.          {     
  198.    for(b=8;b;b--); //8*13.9*3= 333.6     
  199.          }



  200. }
  201. /***********************************

  202. 发送一个字节函数通过串口

  203. ************************************/
  204. void my_send_byte(unsigned char send_date )
  205. {

  206.   while( (USART1->SR&0x00000080)!=0x80);//发送寄存器为空
  207.    USART1->DR=send_date;


  208. }
  209. /**********************************
  210.           初始化串口

  211. **********************************/
  212. void my_USART_init()
  213. {

  214. /*USART2的优先级设为5*/
  215. NVIC->IP[37]=5;
  216. /*开启38号中断即USART2,关闭其他所有外部的中断*/
  217. NVIC->ISER[1]=0x00000020;   
  218. /*设置复用模式下的引脚模式为全双工:TX输出推挽复用,RX为输入上拉模式,速度50MHZ*/
  219.             GPIOA->CRH=0x000008b0;
  220.   /* 1.开启USART,
  221. *
  222. */
  223. USART1->CR1=0x2000;
  224. /* 1.关闭局域网模式
  225. * 2.1个停止位
  226. * 3.CK引脚禁能
  227. */
  228. USART1->CR2=0;
  229. /* 1.关闭调制解调模式
  230. * 2.关闭DMA模式
  231. * 3.关闭智能卡、红外模式
  232. *   4.关闭错误中断

  233. */
  234. USART1->CR3=0;
  235. /*     波特率设置

  236.      2011年8月11日
  237.         王均伟
  238.          天津第四项目部宿舍

  239.     BRR中的第四位(DIV_Fraction)作为小数,高12位(DIV_MANtissa)作为整数部分,
  240.   
  241.     1,根据公式:波特率=fck/16*usardiv,其中usardivBRR寄存器的值,所以变形得:USARDIV=fck/16*波特率
  242.     2.算出来BRR寄存器的值后就要把这个值变成16进制数据写入BRR寄存器中,
  243.       遵循以下规则:
  244.       小数部分*16=DIV_Fraction或者取近似的值
  245.       整数部分直接=DIV_MANtissa
  246.     3.把这个16进制值写入BRR寄存器
  247.     例如我要算波特率设成9600bps的BRR寄存器值,
  248.     1.先求USARDIV=36000000/16*9600=234.375
  249.     2.换成十六进制:DIV_Fraction=16*0.375=0x6
  250.                     DIV_MANtissa=234=0xea
  251.     3.组合并写入寄存器
  252.                      USART2->BRR=0x0ea6;值得注意的是这里是16位半字操作,所以不要以为是32位。

  253. */
  254. USART1->BRR=0x0ea6;

  255. /* 1.开启USART
  256. * 2.开启接收完毕中断
  257. * 3.开启发送功能
  258. *   4.开启接收功能
  259. */
  260. USART1->CR1=0x202c;


  261. }


  262. void mysysinit()//系统初始化程序
  263. {
  264. ErrorStatus HSEStartUpStatus;//说明标志位
  265. RCC_DeInit();//所有外设全部缺省设置

  266. /* Enable HSE */
  267. RCC_HSEConfig(RCC_HSE_ON);
  268. /* Wait till HSE is ready and if Time out is reached exit */
  269. HSEStartUpStatus = RCC_WaitForHSEStartUp();
  270. if(HSEStartUpStatus == SUCCESS)//启动成功
  271. {
  272. /*这两条FLASH指令必须加上,不知为啥?不加上就运行几秒后出错,参照系统初始化*/
  273. /* Enable The Prefetch Buffer */
  274. FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);//FLASH缓存开启
  275. /* Configure the Latency cycle: Set 2 Latency cycles */
  276.   FLASH_SetLatency(FLASH_Latency_2);  //设置FLASH这些位表示SYSCLK(系统时钟)周期与闪存访问时间的比例,为010:两个等待状态,当 48MHz < SYSCLK ≤ 72MHz
  277. /* Set PLL clock output to 72MHz using HSE (8MHz) as entry clock */
  278. RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);//外部时钟为8M,PLL的输入时钟=8MHZ,倍频系数9,

  279. /* Configure HCLK such as HCLK = SYSCLK */
  280. RCC_HCLKConfig(RCC_SYSCLK_Div1);//设置了啦AHB分频器的分频系数=1,即HCLK=SYSCLK=72MHZ
  281. /* Configure PCLK1 such as PCLK1 = HCLK/2 */
  282. RCC_PCLK1Config(RCC_HCLK_Div2);//设置了APB1外设的时钟频率最大是36M这里是APB1的分频器设为2,PCLK1=HCLK/2=72/2=36MHZ正好是最大值
  283. /* Configure PCLK2 such as PCLK2 = HCLK */
  284. RCC_PCLK2Config(RCC_HCLK_Div1);//设置PLCK2=HCLK=72MHZ,的APB2分频器=1
  285. /* Select the PLL as system clock source */
  286. RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);//设置了SYSCLK的提供者为PLL,频率由上面算出=72MHZ
  287. /* disable PLL Ready interrupt */
  288. RCC_ITConfig(RCC_IT_PLLRDY, DISABLE);//PLL中断关闭
  289. /* disable PLL Ready interrupt */
  290. RCC_ITConfig(RCC_IT_HSERDY,DISABLE);//HSE中断关闭
  291. /* disable PLL Ready interrupt */
  292. RCC_ITConfig(RCC_IT_HSIRDY, DISABLE); //HSI中断关闭
  293. /* disable PLL Ready interrupt */
  294. RCC_ITConfig(RCC_IT_LSERDY, DISABLE); //LSE中断关闭
  295. /* disable PLL Ready interrupt */
  296. RCC_ITConfig(RCC_IT_LSIRDY, DISABLE); //LSI中断关闭

  297. /* PLL clock divided by 1.5 used as USB clock source */
  298. RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);//设置USB的时钟为=72、1.5=48mhz
  299. /* Configure ADCCLK such as ADCCLK = PCLK2/2 */
  300. RCC_ADCCLKConfig(RCC_PCLK2_Div2);//设置ADC时钟=PCLK2/2= 36MHZ
  301. /* disable the LSE */
  302. RCC_LSEConfig(RCC_LSE_OFF);//外部低速晶振关闭

  303. /*DISable the RTC clock */
  304. RCC_RTCCLKCmd(DISABLE);
  305. /* DISable the Clock Security System */
  306. RCC_ClockSecuritySystemCmd(DISABLE);
  307. /* Enable the PLL */
  308. RCC_PLLCmd(ENABLE);//使能PLL







  309. /* PLL ans system clock config */
  310. }
  311. else
  312. {
  313. /* Add here some code to deal with this error */
  314. }
  315. }
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表