补充上一次的主函数
#include "stm32f4xx.h"
#include "usart.h"
#include "delay.h"
#include "led.h"
#include "adc.h"
#include "ds18b20.h"
#include "lcd.h"
#include "24l01.h"
#include "timer.h"
/*******************************************
*第一步:adc量通道采集数据显示(显示内部温度)
*第二步:ds18b20温度显示
*第三步:接收命令状态(无线模块)
*第四步:发送数据给另外开发板
*******************************************/
#define SEND_BUF_SIZE 2
u16 ADC1Buff[SEND_BUF_SIZE];
short Get_Temprate(void);
u32 ADC_Convert(u16 *DRValue,u16 i);
void dis_ds18b20(void);
void dis_Adc_value(void);
void u16tou8(u16* u16buff,u8* u8buff,u8 len);
void systeminit()
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
delay_init(168);
uart_init(115200);
TIM3_Int_Init(199,8299);
LED_Init();
LCD_Init();
NRF24L01_Init();
Adc_Init();
MYDMA_Config(DMA2_Stream0,DMA_Channel_0,(u32)&ADC1->DR,(u32)ADC1Buff,SEND_BUF_SIZE);
}
int main(void)
{
u8 tmp_buf[32]={0};
u8 t=0;
systeminit();
LCD_Clear(BLACK);
POINT_COLOR=BLUE;
BACK_COLOR=BLACK;
LCD_ShowString(60+11*8,120,200,16,16,"0.000v");
LCD_ShowString(60+11*8,150,200,16,16,"00.00C");
LCD_ShowString(30+110+8,180,200,16,16,"00.00C");
while(DS18B20_Init())
{
LCD_ShowString(30,130,200,16,16,"DS18B20 Error");
}
while(NRF24L01_Check())
{
LCD_ShowString(30,130,200,16,16,"NRF24L01 Error");
}
while(1)
{
switch(systick_time)
{
case 0: dis_ds18b20(); break;
case 1: dis_Adc_value(); break;
case 2:
{
NRF24L01_RX_Mode();
if(NRF24L01_RxPacket(tmp_buf)==0)
{
t++;
if(tmp_buf[0]=='f')
{
tmp_buf[1]=0;
LCD_ShowString(120+t*16,220,lcddev.width-1,32,16,tmp_buf);
delay_ms(100);
NRF24L01_TX_Mode();
u16tou8(ADC1Buff,tmp_buf,2);
NRF24L01_TxPacket(tmp_buf) ;
}
}
}
break;
}
}
}
u32 ADC_Convert(u16 *DRValue,u16 i)
{
u32 ResultVolt = 0;
for(;i2)
{
ResultVolt +=(DRValue[i]*3300)/4095;
}
ResultVolt/=(SEND_BUF_SIZE/2);
printf("%d\r\n",ResultVolt);
return ResultVolt;
}
short Get_Temprate(void)
{
u16 i=0;
u32 adcx=0;
short result;
double temperate;
for(i=1;i2)
{
adcx+=(u32)ADC1Buff[1];
}
adcx/=(SEND_BUF_SIZE/2);
temperate=(float)adcx*(3.3/4096);
temperate=(temperate-0.76)/0.0025 + 25;
result=temperate*=100;
return result;
}
void dis_ds18b20()
{
short temperature;
u8 t;
u8 x;
x=50;
for(t=12;t<18;t++)
{
Test_Show_CH_Font16(x ,180,t,BLUE);
x+=16;
}
temperature=DS18B20_Get_Temp();
if(temperature<0)
{
LCD_ShowChar(30+110,180,'-',16,0);
temperature=-temperature;
}else LCD_ShowChar(30+110,180,' ',16,0);
LCD_ShowNum(30+110+8,180,temperature/10,2,16);
LCD_ShowNum(30+110+32,180,temperature%10,1,16);
}
void dis_Adc_value()
{
u8 t;
u8 x;
x=50;
for(t=0;t<6;t++)
{
Test_Show_CH_Font16(x ,120,t,BLUE);
x+=16;
}
x=50;
for(t=6;t<12;t++)
{
Test_Show_CH_Font16(x ,150,t,BLUE);
x+=16;
}
LCD_ShowxNum(60+11*8,120,ADC_Convert(ADC1Buff,0)/1000,1,16,0);
LCD_ShowxNum(60+13*8,120,ADC_Convert(ADC1Buff,0)%1000,3,16,0);
LCD_ShowxNum(60+11*8,150,Get_Temprate()/100,2,16,0);
LCD_ShowxNum(60+14*8,150,Get_Temprate()%100,2,16,0);
}
void u16tou8(u16* u16buff,u8* u8buff,u8 len)
{
u8 i;
for(i=0;i2]=(u8)u16buff[i];
u8buff[i*2+1]=(u8)u16buff[i]>>8;
}
}
从机函数
#include "led.h"
#include "delay.h"
#include "key.h"
#include "sys.h"
#include "lcd.h"
#include "usart.h"
#include "24l01.h"
int main(void)
{
u8 key;
u8 tmp_buf[33]={0};
u8 RX_buf[33]={0};
delay_init();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
uart_init(115200);
LED_Init();
KEY_Init();
LCD_Init();
NRF24L01_Init();
POINT_COLOR=RED;
while(NRF24L01_Check())
{
LCD_ShowString(30,130,200,16,16,"NRF24L01 Error");
delay_ms(200);
LCD_Fill(30,130,239,130+16,WHITE);
delay_ms(200);
}
tmp_buf[0]='J';
while(1)
{
key=KEY_Scan(0);
if(key==KEY1_PRES)
{
NRF24L01_TX_Mode();
if(NRF24L01_TxPacket(tmp_buf)==TX_OK)
{
LCD_ShowString(30,120,239,32,16,"Sended DATA:");
LCD_ShowString(50,150,lcddev.width-1,32,16,tmp_buf);
LED0=!LED0;
NRF24L01_RX_Mode();
while(NRF24L01_RxPacket(RX_buf)!=0);
LCD_ShowString(30,170,200,16,16,"Received DATA:");
RX_buf[5]=0;
LCD_ShowString(50,190,lcddev.width-1,32,16,RX_buf);
LED1=!LED1;
}
}
}
}