[经验]
【Nucleo G071评测】I2C OLED&AD采集
借助一块庆科出品的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*/
}
录入字库之后看看显示效果:
然后是带了AD采集的光照强度传感器,占用接口为PA4,这里需要通过CubeMX配置ADC接口:
移植官方例程生成的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电压值会随着光照强度变化:
暂无评论,赶紧抢沙发吧