主页 > 其他 > test

test

2014年6月3日 test无评论 阅读: 9,511 次

#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

typedef unsigned char uint8;
typedef unsigned int uint16;
typedef unsigned long uint32;

int Max7219_pinCLK = 3;
int Max7219_pinCS = 4;
int Max7219_pinDIN = 5;

int i2cAddress=0x5;

#define HIGH_TIME 32

int pinNumber =1; //use gpio1 to read data
uint32 databuf;
int led0=0;
int maxc=0;
int maxa=2000;

unsigned char leddata[10]={0x7e,0x30,0x6d,0x79,0x33,0x5b,0x5f,0x70,0x7f,0x7b};
// the setup routine runs once when you press reset:

//功能:向MAX7219(U3)写入字节
//入口参数:DATA
//出口参数:无
//说明:
void Write_Max7219_byte(unsigned char DATA)
{
unsigned char i;

digitalWrite(Max7219_pinCS,0);
for(i=8;i>=1;i--)
{
digitalWrite(Max7219_pinCLK,0);
/* if(DATA&0x80==0)
digitalWrite(Max7219_pinDIN,0);
else
digitalWrite(Max7219_pinDIN,1);*/
digitalWrite(Max7219_pinDIN,(DATA&0x80));
DATA=DATA<<1;
digitalWrite(Max7219_pinCLK,1);
}
}
//-------------------------------------------
//功能:向MAX7219写入数据
//入口参数:address、dat
//出口参数:无
//说明:
void Write_Max7219(unsigned char address,unsigned char dat)
{
digitalWrite(Max7219_pinCS,0);
Write_Max7219_byte(address); //写入地址,即数码管编号
Write_Max7219_byte(dat); //写入数据,即数码管显示数字
digitalWrite(Max7219_pinCS,1);
}
void outledlx(unsigned int val)
{
unsigned int tt;
Write_Max7219(8,0xe);//"L"
Write_Max7219(7,0);//
tt=val/10000;
Write_Max7219(6,leddata[tt]);//
val=val-tt*10000;
tt=val/1000;
Write_Max7219(5,leddata[tt]);//
val=val-tt*1000;
tt=val/100;
Write_Max7219(4,leddata[tt]);//
val=val-tt*100;
tt=val/10;
Write_Max7219(3,leddata[tt]);//
Write_Max7219(2,leddata[val%10]);//
Write_Max7219(1,0);//
}
outdate(int year,int mon,int date)
{
unsigned int tt;
tt=year/1000;
Write_Max7219(8,leddata[tt]);//
year=year-tt*1000;
tt=year/100;
Write_Max7219(7,leddata[tt]);//
year=year-tt*100;
tt=year/10;
Write_Max7219(6,leddata[tt]);//
Write_Max7219(5,leddata[year%10]);//
tt=mon/10;
Write_Max7219(4,leddata[tt]);//
Write_Max7219(3,leddata[mon%10]);//
tt=date/10;
Write_Max7219(2,leddata[tt]);//
Write_Max7219(1,leddata[date%10]);//
}
outtime(int hour,int min,int sec)
{
unsigned int tt;

tt=hour/10;
Write_Max7219(8,leddata[tt]);//
Write_Max7219(7,leddata[hour%10]);//
Write_Max7219(6,0x80);//"."
tt=min/10;
Write_Max7219(5,leddata[tt]);//
Write_Max7219(4,leddata[min%10]);//
Write_Max7219(3,0x80);//"."
tt=sec/10;
Write_Max7219(2,leddata[tt]);//
Write_Max7219(1,leddata[sec%10]);//
}
void outled(int temp,int shi)
{
if(temp>=100)
temp=99;
if(temp<=-100)
temp=-99;
if(shi<0)
shi=0;
if(shi>=100)
shi=99;
if(temp<0)
{
temp=-temp;
Write_Max7219(8,1);//"-"
Write_Max7219(7,leddata[temp/10]);
Write_Max7219(6,leddata[temp%10]);
Write_Max7219(5,0x4e);//"C"
}
else
{
Write_Max7219(8,leddata[temp/10]);
Write_Max7219(7,leddata[temp%10]);
Write_Max7219(6,0x4e);//"C"
Write_Max7219(5,0);//null
}
Write_Max7219(4,leddata[shi/10]);
Write_Max7219(3,leddata[shi%10]|0x80);
Write_Max7219(2,0);//"."
Write_Max7219(1,0);//null
}

void Init_MAX7219(void)
{
Write_Max7219(0x09, 0x00); //译码方式:BCD码
Write_Max7219(0x0a, 0x01); //亮度
Write_Max7219(0x0b, 0x07); //扫描界限;8个数码管显示
Write_Max7219(0x0c, 0x01); //掉电模式:0,普通模式:1
Write_Max7219(0x0f, 0x00); //显示测试:1;测试结束,正常显示:0
}

void setup() {
// initialize the digital pin as an output.
pinMode(Max7219_pinCLK, OUTPUT);
pinMode(Max7219_pinCS, OUTPUT);
pinMode(Max7219_pinDIN, OUTPUT);

delay(50);
Init_MAX7219();
delay(500);

}

uint8 readSensorData(void)
{
uint8 crc;
uint8 i;
int tempc=0;

pinMode(pinNumber,OUTPUT); // set mode to output
digitalWrite(pinNumber, 0); // output a high level
delay(25);
digitalWrite(pinNumber, 1); // output a low level
pinMode(pinNumber, INPUT); // set mode to input
pullUpDnControl(pinNumber,PUD_UP);

delayMicroseconds(27);
if(digitalRead(pinNumber)==0) //SENSOR ANS
{
while(!digitalRead(pinNumber)&&tempc<maxa)
{
tempc++;
}
if(tempc>maxc)
maxc=tempc;
tempc=0;
//wait to high
for(i=0;i<32;i++)
{
while(digitalRead(pinNumber)&&tempc<maxa)
{
tempc++;
}
if(tempc>maxc)
maxc=tempc;
tempc=0;
//data clock start
while(!digitalRead(pinNumber)&&tempc<maxa)
{
tempc++;
}
if(tempc>maxc)
maxc=tempc;
tempc=0;
//data start
delayMicroseconds(HIGH_TIME);
databuf*=2;
if(digitalRead(pinNumber)==1) //1

{
databuf++;
}
}

for(i=0;i<8;i++)
{
while(digitalRead(pinNumber)&&tempc<maxa)
{
tempc++;
}
if(tempc>maxc)
maxc=tempc;
tempc=0;
//data clock start
while(!digitalRead(pinNumber)&&tempc<maxa)
{
tempc++;
}
if(tempc>maxc)
maxc=tempc;
tempc=0;
//data start
delayMicroseconds(HIGH_TIME);
crc*=2;
if(digitalRead(pinNumber)==1) //1
{
crc++;
}
}
return 1;
}
else
{
return 0;
}
}

int main (void)
{
time_t now; //实例化time_t结构
struct tm *timenow; //实例化tm结构指针
char mcmd[300];
char lcmd[100];
char tcmd[30];
int cc=0;
int tempc,tempb;
int lastb=0,lastc=0,t1,t2;
unsigned int i2cval;
int fd;

printf("Use GPIO1 to read data!\n");

if (-1 == wiringPiSetup()) {
printf("Setup wiringPi failed!");
return 1;
}

if ((fd = wiringPiI2CSetup (i2cAddress)) < 0)
{
printf("i2c err\n");
return 1 ;
}

//7219
setup();
pinMode (0, OUTPUT) ;
led0=0;

pinMode(pinNumber, OUTPUT); // set mode to output
digitalWrite(pinNumber, 1); // output a high level
printf("Enter OS-------\n");
while(1) {
pinMode(pinNumber,OUTPUT); // set mode to output
digitalWrite(pinNumber, 1); // output a high level
delay(3000);

if(readSensorData())
{
time(&now);//time函数读取现在的时间(国际标准时间非北京时间),然后传值给now
timenow = localtime(&now);//localtime函数把从time取得的时间now换算成你电脑中的时间(就是你设置的地区)
printf("Local time is %s[%d]:[%d]\n",asctime(timenow),cc,maxc);//asctime函数把时间转换成字符,通过printf()函数输出
sprintf(tcmd,"%d-%d-%d %d:%d:%d\n",(1900+timenow->tm_year),( 1+timenow->tm_mon),(timenow)->tm_mday,timenow->tm_hour, timenow->tm_min, timenow->tm_sec);

//printf("Congratulations ![%d] ok![%d]\n",cc,maxc);

tempc=(databuf>>8)&0xff;
tempb=(databuf>>24)&0xff;
int dispt=cc%4;
switch(dispt)
{
case 0:
i2cval=wiringPiI2CReadReg16(fd,0);
outledlx(i2cval);
break;
case 1:
outled(tempc,tempb);
break;
case 2:
outdate((1900+timenow->tm_year),( 1+timenow->tm_mon),(timenow)->tm_mday);
break;
case 3:
outtime(timenow->tm_hour, timenow->tm_min, timenow->tm_sec);
break;
}
printf("RH:%d.%d\n",tempb,(databuf>>16)&0xff);
printf("TMP:%d.%d\n",tempc,databuf&0xff);
sprintf(mcmd,"curl --request POST http://www.lewei50.com/api/V1/Gateway/UpdateSensors/02 --data \"[{'Name':'wen','Value':'%d'},{'Name':'shi','Value':'%d'}]\" --header \"userkey:0db3c490ac624e92b7334e84daa6693f\"",tempc,tempb);
//printf("%s\n",mcmd);
sprintf(lcmd,"curl -d \"wen=%d&shi=%d&time=%s\" \"http://127.0.0.1/in.php\"" ,tempc,tempb,tcmd);
//printf("%s\n",lcmd);

if(!(cc%10)&&tempc<42&&tempc>-40&&tempb<100&&tempb>0&&cc!=0)
{
t1=lastb-tempb;
t2=lastc-tempc;
if(t1<10&&t1>-10&&t2<10&&t2>-10)
{
//printf("[%d],[%d]\n",lastb,tempb);
//printf("[%d],[%d]\n",lastc,tempc);
system(mcmd);
system(lcmd);
}
lastb=tempb;
lastc=tempc;
}
cc++;
digitalWrite (0, led0) ;
if(led0==0)
led0=1;
else
led0=0;
databuf=0;
}
else
{
printf("Sorry! Sensor dosent ans!\n");
databuf=0;
}
}
return 0;
}

发表评论

新用户的评论需审核后才会显示;

电子邮件地址不会被公开;
必填项已用*标注