社区导航

 

搜索
查看: 425|回复: 0

[经验] 【Nucleo G071评测】I2C OLED&AD采集

[复制链接]

122

TA的帖子

0

TA的资源

一粒金砂(中级)

Rank: 2

发表于 2019-1-24 00:15 | 显示全部楼层 |阅读模式
    借助一块庆科出品的Arduino扩展板,插上Nucleo-G071板子之后,可以继续进行接下来的传感器实验。
    首先是这块扩展板是自带了I2C接口的OLED扩展模块,占用接口为PB8 PB9,直接用模拟I2C时序进行通信即可:
void OLED_WR_Byte(uint8_t data,char cmd)
{
        if(cmd)
        {
                // HAL_I2C_Mem_Write(&hi2c1,0x78,0x40,1,&data,1,10);
                I2C_WriteData(0x78,data,0x40);
        }
  else
        {
                //HAL_I2C_Mem_Write(&hi2c1,0x78,0,1,&data,1,10);
                I2C_WriteData(0x78,data,0);
        }
}


void OLED_Set_Pos(char x,char y)
{
  uint8_t tmp[3] = {0xb0+y, ((x&0xf0)>>4)|0x10, (x&0x0f)|0x01};
  OLED_WR_Byte(tmp[0],0);
        OLED_WR_Byte(tmp[1],0);
        OLED_WR_Byte(tmp[2],0);
}


void OLED_Clear(void)  
{  
        int i,n;                    
        for(i=0;i<8;i++)  
        {  
                OLED_WR_Byte(0xb0+i,0);
                OLED_WR_Byte(0x00,0);
                OLED_WR_Byte(0x10,0);
                for(n=0;n<128;n++)OLED_WR_Byte(0,1);
        }
}


void OLED_Fill(void)  
{  
        int i,n;                    
        for(i=0;i<8;i++)  
        {  
                OLED_WR_Byte(0xb0+i,0);
                OLED_WR_Byte(0x00,0);
                OLED_WR_Byte(0x10,0);
                for(n=0;n<128;n++)OLED_WR_Byte(0xff,1);
        }
}


void OLED_ShowChinese(int x,int y,int n)
{
        int i=0;
        n*=32;
        OLED_Set_Pos(x,y);
        for(i=n;i
        {
                OLED_WR_Byte(CnChar,1);
        }
        OLED_Set_Pos(x,y+1);
        for(i=n+16;i
        {
                OLED_WR_Byte(CnChar,1);
        }
}


void OLED_ShowASCII(int x,int y,char n)
{
        int i=0,m;
        n-=0x20;
        m=n*16;
        OLED_Set_Pos(x,y);
        for(i=m;i
        {
                OLED_WR_Byte(ASCIIChar,1);
        }
        OLED_Set_Pos(x,y+1);
        for(i=m+8;i
        {
                OLED_WR_Byte(ASCIIChar,1);
        }
}


void OLED_ShowString(int x,int y,char s[])
{
        int i;
        for(i=0;s!='\0';i++)
                OLED_ShowASCII(x+i*8,y,s);
       
}


void OLED_Init()
{
        //I2C1_Init();
        I2C_GPIO_Init();
  OLED_WR_Byte(0xAE,0);//--turn off oled panel
  OLED_WR_Byte(0x00,0);//---set low column address
  OLED_WR_Byte(0x10,0);//---set high column address
  OLED_WR_Byte(0x40,0);//--set start line address  Set Mapping RAM Display Start Line (0x00~0x3F)
  OLED_WR_Byte(0x81,0);//--set contrast control register
  OLED_WR_Byte(0xCF,0); // Set SEG Output Current Brightness
  OLED_WR_Byte(0xA1,0);//--Set SEG/Column Mapping     0xa0?????????? 0xa1??????
  OLED_WR_Byte(0xC8,0);//Set COM/Row Scan Direction   0xc0?????????? 0xc8??????
  OLED_WR_Byte(0xA6,0);//--set normal display
  OLED_WR_Byte(0xA8,0);//--set multiplex ratio(1 to 64)
  OLED_WR_Byte(0x3f,0);//--1/64 duty
  OLED_WR_Byte(0xD3,0);//-set display offset        Shift Mapping RAM Counter (0x00~0x3F)
  OLED_WR_Byte(0x00,0);//-not offset
  OLED_WR_Byte(0xd5,0);//--set display clock divide ratio/oscillator frequency
  OLED_WR_Byte(0x80,0);//--set divide ratio, Set Clock as 100 Frames/Sec
  OLED_WR_Byte(0xD9,0);//--set pre-charge period
  OLED_WR_Byte(0xF1,0);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
  OLED_WR_Byte(0xDA,0);//--set com pins hardware configuration
  OLED_WR_Byte(0x12,0);
  OLED_WR_Byte(0xDB,0);//--set vcomh
  OLED_WR_Byte(0x40,0);//Set VCOM Deselect Level
  OLED_WR_Byte(0x20,0);//-Set Page Addressing Mode (0x00/0x01/0x02)
  OLED_WR_Byte(0x02,0);//
  OLED_WR_Byte(0x8D,0);//--set Charge Pump enable/disable
  OLED_WR_Byte(0x14,0);//--set(0x10) disable
  OLED_WR_Byte(0xA4,0);// Disable Entire Display On (0xa4/0xa5)
  OLED_WR_Byte(0xA6,0);// Disable Inverse Display On (0xa6/a7)   
  
  OLED_Clear();
  OLED_Set_Pos(0,0);        
  OLED_WR_Byte(0xAF,0); /*display ON*/

}

录入字库之后看看显示效果:
IMG_20190124_000221.jpg
然后是带了AD采集的光照强度传感器,占用接口为PA4,这里需要通过CubeMX配置ADC接口:
15.jpg
移植官方例程生成的ADC初始化代码:
ADC_ChannelConfTypeDef sConfig;
ADC_HandleTypeDef hadc1;
GPIO_InitTypeDef GPIO_InitStruct;


void ADC_Init()
{
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_ADC_CLK_ENABLE();
  GPIO_InitStruct.Pin = GPIO_PIN_4;
  GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
       


  hadc1.Instance = ADC1;
  hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
  hadc1.Init.Resolution = ADC_RESOLUTION_12B;
  hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
  hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  hadc1.Init.LowPowerAutoWait = DISABLE;
  hadc1.Init.LowPowerAutoPowerOff = DISABLE;
  hadc1.Init.ContinuousConvMode = DISABLE;
  hadc1.Init.NbrOfConversion = 1;
  hadc1.Init.DiscontinuousConvMode = DISABLE;
  hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  hadc1.Init.DMAContinuousRequests = DISABLE;
  hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;
  hadc1.Init.SamplingTimeCommon1 = ADC_SAMPLETIME_1CYCLE_5;
  hadc1.Init.SamplingTimeCommon2 = ADC_SAMPLETIME_1CYCLE_5;
  hadc1.Init.OversamplingMode = DISABLE;
  hadc1.Init.TriggerFrequencyMode = ADC_TRIGGER_FREQ_HIGH;
  HAL_ADC_Init(&hadc1);


}


int ADC1_GetValue()   
{   
        int val;
        sConfig.Channel=ADC_CHANNEL_4;
  sConfig.Rank = ADC_REGULAR_RANK_1;
  sConfig.SamplingTime = ADC_SAMPLINGTIME_COMMON_1;
        HAL_ADC_ConfigChannel(&hadc1,&sConfig);
  if(HAL_ADC_Start(&hadc1)==HAL_OK)
        {
                val=(int)HAL_ADC_GetValue(&hadc1);
                return val;
        }
        else return -1;

}
观察效果,光照强度传感器输出的ADC电压值会随着光照强度变化:
2.gif

来源:EEWorld stm32/stm8板块,转载请附上链接


回复

使用道具 举报

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

关闭

站长推荐上一条 /6 下一条

  • 论坛活动 E手掌握

    扫码关注
    EEWORLD 官方微信

  • EE福利  唾手可得

    扫码关注
    EE福利 唾手可得

Archiver|手机版|小黑屋|电子工程世界 ( 京ICP证 060456 )

GMT+8, 2019-9-21 20:56 , Processed in 0.564680 second(s), 12 queries , Gzip On, MemCache On.

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