一、数码管
具体可以点击看一下,74ls595锁存器及其用法
74LS595引脚功能说明void SEG_DisplayValue(u8 Bit1, u8 Bit2, u8 Bit3)
{
u8 i = 0; //
u8 code_tmp = 0;
code_tmp = Seg7[Bit3];
for(i=0;i<8;i++){
if(code_tmp & 0x80){ //如果显示 1(0x06) :0000 0110 & 1000 0000 高位选中,打开段选
SER_H; //开启段选,打开
}else{
SER_L; //关闭段选
}
SCK_H;//开始移位,QA->QH
code_tmp = code_tmp << 1;//移位,寻找到高位
SCK_L;//关闭移位,保持数据
}
//八位满了之后:QH·与SER保持一致电平,进行第二个芯片的段选
code_tmp = Seg7[Bit2];
for(i=0;i<8;i++){
if(code_tmp & 0x80){
SER_H;
}else{
SER_L;
}
SCK_H;
code_tmp = code_tmp << 1;
SCK_L;
}
code_tmp = Seg7[Bit1]|0x80; //左高右低,DP 是最高位,小数点显示
for(i=0;i<8;i++){
if(code_tmp & 0x80){
SER_H;
}else{
SER_L;
}
SCK_H;
code_tmp = code_tmp << 1;
SCK_L;
}
RCLK_H;
RCLK_L;
}
二、ADC矩阵按键
这个按键程序没难度,直接测ADC的值就好,然后不犯低级错误,问题不大
u8 ScanButton(float value)
{
static u8 key = 0;
if(value>0.70&&value<0.79)key = 1;
else if(value>1.30&&value<1.39)key = 2;
else if(value>1.70&&value<1.80)key = 3;
else if(value>0.50&&value<0.60)key = 4;
else if(value>1.20&&value<1.30)key = 5;
else if(value>1.60&&value<1.70)key = 6;
else if(value>0.25&&value<0.35)key = 7;
else if(value>1.00&&value<1.15)key = 8;
else if(value>1.50&&value<1.60)key = 9;
else if(value>0.01&&value<0.10)key = 10;
else if(value>0.80&&value<1.00)key = 11;
else if(value>1.40&&value<1.50)key = 12;
return key;
}
三、DS18B20
(1)浮点型
float ReadTemp(void)
{
int temp = 0,high = 0,low = 0;
ow_reset();
ow_byte_wr(0xcc); //跳过读
ow_byte_wr(0x44); //温度转换
delay(750000);
ow_reset();
ow_byte_wr(0xcc); //跳过读
ow_byte_wr(0xbe); //温度转换
low = ow_byte_rd(); //先读低八位
high = ow_byte_rd();//后读高八位
temp = (high&0x0f)<<8|low ;//数值转换
return temp*0.0625;
}
(2)整型
u8 ds18b20_read(void)
{
u8 val[2];
u8 i = 0;
s16 x = 0;
ow_reset();
ow_byte_wr(OW_SKIP_ROM); //0xcc 跳过读
ow_byte_wr(DS18B20_CONVERT); //0x44 转换温度值
delay_us(750000);
ow_reset();
ow_byte_wr( OW_SKIP_ROM ); //0xcc 跳过读
ow_byte_wr ( DS18B20_READ ); //0xbe 转换温度值
for ( i=0 ;i<2; i++) { //val[0]是低八位 先读
val[i] = ow_byte_rd(); //val[1]是高八位 后读
}
x = val[1]; //整数型数据
x <<= 4;
x |= val[0]>>4;
return x;
}
四、DHT11
这个程序相对DS18B20较难
unsigned int dht11_read(void)
{
int i;
long long val;
int timeout;
GPIO_ResetBits(GPIOA, GPIO_Pin_7);
delay_us(18000); //pulldown for 18ms
GPIO_SetBits(GPIOA, GPIO_Pin_7);
delay_us( 20 ); //pullup for 30us
mode_input();
//等待DHT11拉高,80us
timeout = 5000;
while( (! GPIO_ReadInputDataBit (GPIOA, GPIO_Pin_7)) && (timeout > 0) ) timeout--; //wait HIGH
//等待DHT11拉低,80us
timeout = 5000;
while( GPIO_ReadInputDataBit (GPIOA, GPIO_Pin_7) && (timeout > 0) ) timeout-- ; //wait LOW
#define CHECK_TIME 28
for(i=0;i<40;i++)
{
timeout = 5000;
while( (! GPIO_ReadInputDataBit (GPIOA, GPIO_Pin_7)) && (timeout > 0) ) timeout--; //wait HIGH
delay_us(CHECK_TIME);
if ( GPIO_ReadInputDataBit (GPIOA, GPIO_Pin_7) )
{
val=(val<<1)+1;
} else {
val<<=1;
}
timeout = 5000;
while( GPIO_ReadInputDataBit (GPIOA, GPIO_Pin_7) && (timeout > 0) ) timeout-- ; //wait LOW
}
mode_output();
GPIO_SetBits(GPIOA, GPIO_Pin_7);
if (((val>>32)+(val>>24)+(val>>16)+(val>>8) -val ) & 0xff ) return 0;
else return val>>8;
}
五、光敏电阻
光敏电阻从原理图上可以看出AO端可以测量电压,反推出电阻;DO端仅仅进行比较输出,高低电平
1 . AO
tmp = Read_ADC();
snprintf((char *)str, sizeof(str), " R-P:%.2fK ", tmp/(4096.-tmp)*10);
2 . DO
void DO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;//例程的代码
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_3) == Bit_RESET){
LCD_DisplayStringLine(Line7, (u8*)" DO:High ");
}else{
LCD_DisplayStringLine(Line7, (u8*)" DO:Low ");
}
六、其他的是脉冲生成器与0~3.3V的电位器
七、重点在这
扩展板全部代码(官方版)连接——密码:uxu1
另:流水灯
if(flag400ms)
{
flag400ms = 0;
if(led_num<15)led_num++;
else led_num = 12;
}
GPIOC->BRR = 1 << led_num;//左移
GPIOD->BSRR = GPIO_Pin_2;//锁存器
GPIOD->BRR = GPIO_Pin_2;
网友评论