找回密码
 立即注册

QQ登录

只需一步,快速开始

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

STM32单片机lcd16c02+adc程序

[复制链接]
ID:967993 发表于 2021-11-1 14:53 | 显示全部楼层 |阅读模式
  1. // LCD1602.h

  2. #ifndef _BSP_LCD1602_H
  3. #define _BSP_LCD1602_H

  4. #include "stm32f10x.h"
  5. #include "stm32f10x_gpio.h"
  6. #include "bsp_SysTick.h"

  7. #define LCD1602_CLK  RCC_APB2Periph_GPIOB  

  8. #define LCD1602_GPIO_PORT   GPIOB

  9. #define LCD1602_E    GPIO_Pin_10              //定义使能引脚
  10. #define LCD1602_RW   GPIO_Pin_11             //定义读写引脚
  11. #define LCD1602_RS   GPIO_Pin_12             //定义数据、命名引脚



  12. #define EO(X)         X? (GPIO_SetBits(LCD1602_GPIO_PORT,LCD1602_E)):(GPIO_ResetBits(LCD1602_GPIO_PORT,LCD1602_E))
  13. #define RWO(X)        X? (GPIO_SetBits(LCD1602_GPIO_PORT,LCD1602_RW)):(GPIO_ResetBits(LCD1602_GPIO_PORT,LCD1602_RW))
  14. #define RSO(X)        X? (GPIO_SetBits(LCD1602_GPIO_PORT,LCD1602_RS)):(GPIO_ResetBits(LCD1602_GPIO_PORT,LCD1602_RS))

  15. //只能是某个GPIO口的低八位
  16. #define DB0                                        GPIO_Pin_0
  17. #define DB1                                        GPIO_Pin_1
  18. #define DB2                                        GPIO_Pin_2
  19. #define DB3                                        GPIO_Pin_3
  20. #define DB4                                        GPIO_Pin_4
  21. #define DB5                                        GPIO_Pin_5
  22. #define DB6                                        GPIO_Pin_6
  23. #define DB7                                        GPIO_Pin_7

  24. void LCD1602_Init(void);  //初始化LCD602;
  25. void LCD1602_ShowStr(uint8_t x, uint8_t y, uint8_t *str,uint8_t len);
  26. void LCD_ShowNum(uint8_t x, uint8_t y,uint8_t num);
  27. void LCD_ShowChar(uint8_t x, uint8_t y,uint8_t dat);







  28. #endif //_BSP_LCD1602_H
复制代码

#include "adc.h"

void ADC1_GPIO_Config(void){
    GPIO_InitTypeDef GPIO_InitStructure;
          RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_ADC1, ENABLE);        //使能ADC1,GPIOC时钟
           
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; //
    //GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//为什么没有配置这个????
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;        //模拟输入
    GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化PC4
}


void ADC_Config(void)
{
        

  ADC_InitTypeDef ADC_InitStructure;//ADC结构体变量//注意在一个语句快内变量的声明要放在可执行语句的前面,否则出错,因此要放在ADC1_GPIO_Config();前面
  ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;//ADC1和ADC2工作在独立模式
  ADC_InitStructure.ADC_ScanConvMode =        DISABLE; //使能扫描
  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;//ADC转换工作在连续模式
  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;//由软件控制转换,不使用外部触发
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//转换数据右对齐
  ADC_InitStructure.ADC_NbrOfChannel = 1;//转换通道为1
  ADC_Init(ADC1, &ADC_InitStructure); //初始化ADC
        
  ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_55Cycles5);
  //ADC1选择信道14,音序等级1,采样时间55.5个周期
//  ADC_DMACmd(ADC1, ENABLE);//使能ADC1模块DMA
  ADC_Cmd(ADC1, ENABLE);//使能ADC1
        ADC_ITConfig(ADC1, ADC_IT_EOC, ENABLE);
  ADC_SoftwareStartConvCmd(ADC1, ENABLE);
//  ADC_ResetCalibration(ADC1); //重置.(复位).ADC1校准寄存器
//  while(ADC_GetResetCalibrationStatus(ADC1));//等待ADC1校准重置完成
//  ADC_StartCalibration(ADC1);//开始ADC1校准
//  while(ADC_GetCalibrationStatus(ADC1));//等待ADC1校准完成
//  ADC_SoftwareStartConvCmd(ADC1, ENABLE); //使能ADC1软件开始转换
}


  1. //adc.h
  2. #ifndef  __ADC_H
  3. #define  __ADC_H
  4. #include "sys.h"

  5. void Adc_Init(void);
  6. void ADC1_GPIO_Config(void);
  7. void ADC_Config(void);


  8. #endif
复制代码
  1. #include "bsp-lcd1602.h"

  2. void LCD1602_GPIO_Config(void)
  3. {
  4.         RCC_APB2PeriphClockCmd(LCD1602_CLK, ENABLE);
  5.         GPIO_InitTypeDef LCD1602_GPIOStruct;
  6.         LCD1602_GPIOStruct.GPIO_Mode = GPIO_Mode_Out_PP;
  7.         LCD1602_GPIOStruct.GPIO_Speed = GPIO_Speed_10MHz;
  8.         LCD1602_GPIOStruct.GPIO_Pin =  LCD1602_E | LCD1602_RS | LCD1602_RW ;
  9.         GPIO_Init(LCD1602_GPIO_PORT,&LCD1602_GPIOStruct);
  10.         LCD1602_GPIOStruct.GPIO_Mode = GPIO_Mode_Out_OD;
  11.         LCD1602_GPIOStruct.GPIO_Pin =   DB0 |  DB1 | DB2 |DB3 |  DB4 | DB5|
  12.                                                                                                                                         DB6 |  DB7 ;     //设置为开漏输出
  13.         GPIO_Init(LCD1602_GPIO_PORT,&LCD1602_GPIOStruct);
  14. }

  15. void LCD1602_WaitReady(void) //检测忙状态
  16. {
  17.         uint8_t sta;

  18.         GPIOB->ODR =0x00FF;
  19.         RSO(0);
  20.         RWO(1);
  21.         EO(1);
  22.         SysTick_Delay_Us(1);
  23.         do{
  24.                 sta=GPIO_ReadInputDataBit(LCD1602_GPIO_PORT,GPIO_Pin_7);
  25.                 EO(0);
  26.         }while(sta);
  27. }

  28. void LCD1602_WriteCmd(uint8_t cmd) //写指令
  29. {
  30.         LCD1602_WaitReady();
  31.         RSO(0);
  32.         RWO(0);
  33.         EO(0);
  34.         SysTick_Delay_Us(1);
  35.         EO(1);
  36.         LCD1602_GPIO_PORT->ODR &= (cmd|0xFF00);
  37.         EO(0);
  38.         SysTick_Delay_Us(400);
  39. }

  40. void LCD1602_WriteDat(uint8_t dat) //写数据
  41. {
  42.         LCD1602_WaitReady();
  43.         RSO(1);
  44.         RWO(0);
  45.         SysTick_Delay_Us(30);
  46.         EO(1);
  47.         LCD1602_GPIO_PORT->ODR &=(dat|0xFF00);
  48.         EO(0);
  49.         SysTick_Delay_Us(400);
  50. }

  51. void LCD1602_SetCursor(uint8_t x, uint8_t y)
  52. {
  53.     uint8_t addr;
  54.    
  55.     if (y == 0)  //由输入的屏幕坐标计算显示RAM的地址
  56.         addr = 0x00 + x;  //第一行字符地址从0x00起始
  57.     else
  58.         addr = 0x40 + x;  //第二行字符地址从0x40起始
  59.     LCD1602_WriteCmd(addr|0x80);  //设置RAM地址
  60. }

  61. void LCD1602_ShowStr(uint8_t x, uint8_t y, uint8_t *str, uint8_t len)
  62. {
  63.     LCD1602_SetCursor(x, y);        //设置起始地址
  64.     while (len--)         //连续写入len个字符数据
  65.     {
  66.         LCD1602_WriteDat(*str++);
  67.     }
  68. }

  69. //??1???
  70. //x,y :????         
  71. //num:??(0~99)         
  72. //-----------------------------*/         
  73. void LCD_ShowNum(uint8_t x, uint8_t y,uint8_t num)
  74. {     

  75.             LCD1602_SetCursor(x, y);        //设置起始地址
  76.     LCD_ShowChar(x,y,num+'0');
  77.         
  78. }

  79. void LCD_ShowChar(uint8_t x, uint8_t y,uint8_t dat)
  80. {

  81.             LCD1602_SetCursor(x, y);        //设置起始地址
  82.         LCD1602_WriteDat(dat);
  83. }



  84. void LCD1602_Init(void)
  85. {
  86.           LCD1602_GPIO_Config();   //开启GPIO口
  87.     LCD1602_WriteCmd(0X38);  //16*2显示,5*7点阵,8位数据接口
  88.     LCD1602_WriteCmd(0x0C);  //显示器开,光标关闭
  89.     LCD1602_WriteCmd(0x06);  //文字不动,地址自动+1
  90.     LCD1602_WriteCmd(0x01);  //清屏
  91. }
  92.         
  93.         



复制代码
  1. int main()
  2. #include "stm32f10x.h"
  3. #include "bsp-lcd1602.h"
  4. #include "delay.h"
  5. #include "sys.h"
  6. #include "adc.h"

  7. int main(void)
  8. {
  9. int a,b,c,d;
  10.         float temp;
  11.         
  12.         delay_init();                     //延时函数初始化                  
  13.         LCD1602_Init();
  14.   ADC1_GPIO_Config();
  15.   ADC_Config();  
  16.         LCD1602_ShowStr(1,0,"adcvalue=0.0V",13);
  17.         LCD1602_ShowStr(1,1,"LIUTONGBO 32",13);
  18.         
  19.         while(1)
  20.         {
  21.                 b=ADC_GetConversionValue(ADC1);
  22.         temp=(float)b*(3.4/4096);
  23.         a=temp/0.675;
  24.         c=temp*10;
  25.         d=c%10;
  26.         LCD_ShowNum(10,0,a);
  27.         LCD_ShowNum(12,0,d);
  28.         }
  29. }



复制代码


评分

参与人数 1黑币 +20 收起 理由
admin + 20 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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