单片机:ATMEGA16A-PU,WIFI模块:ESP8266,LCD1602液晶 实现网络调时,IDE是用CVAVR;后端是在Linux平台上开发的!,后端是从网上复制下来的,稍作些修改!
还有许多不完整的地方!希望大神们给出建议!
main.c
- #include "lcd.h"
- #include "mcu.h"
- #include
- #include
- #include
- #include
- /******************±äÁ¿¶¨Òå start*****************************/
- typedef struct time{
- unsigned char sec;
- unsigned char min;
- unsigned char hour;
- unsigned char buf[8]; //´®¿ÚÊý¾Ý´æ´¢
- long int tm;
- } Time;
- Time t1 = {0, 0, 0, 0, 0}; //³õʼ»¯½á¹¹Ìå
- /*
- * status ±íʾÏÔʾ״̬
- * 1£º×Ö·û´®½ÓÊÜÍê±Ï
- * 2£ºWiFi¿ªÊ¼·¢ËÍ
- */
- static int status = 0; //״̬
- /******************±äÁ¿¶¨Òå end*****************************/
- /******************º¯Êý¶¨Òå start*****************************/
- void DisPlay(void){
- DisChar(2, 4, t1.hour/10 + 0x30);
- DisChar(2, 5, t1.hour%10 + 0x30);
- DisChar(2, 6, ':');
- DisChar(2, 7, t1.min/10 + 0x30);
- DisChar(2, 8, t1.min%10 + 0x30);
- DisChar(2, 9, ':');
- DisChar(2, 10, t1.sec/10 + 0x30);
- DisChar(2, 11, t1.sec%10 + 0x30);
- }
- void Connect(void){
- SREG = 0X00; //ÖжϹرÕ
- printf("AT+RSTn"); //WiFiÖØÖÃ
- delay_ms(5000); //ÑÓʱ5s
- printf("AT+CWJAP="CHINA_NET","wulian&CKKJ123"n"); //Á´½Ó¾ÖÓòÍø
- delay_ms(10000); //ÑÓʱ5s
- printf("AT+CIPSTART="TCP","192.168.0.102",8086n");//Á´½Ó·þÎñÆ÷
- //delay_ms(500);
- delay_ms(500); //ÑÓʱ5s
- SREG = 0X80; //ÖжϿªÆô
- }
- /******************º¯Êý¶¨Òå end*****************************/
- void main(void){
- delay_ms(500); //µÈ´ýµçÔ´Îȶ¨
- McuInit();
- TIMSK = 0X04; //¿ªÊ¼¼Æʱ
- LcdInit();
- SREG = 0X80; //È«¾ÖÖжÏ
- while(1){
- DisString(1, 3, "The time is");
- if(status == 0){
- DisPlay();
- }
- if(status == 1){
- /* ´¦Àí´®¿Ú´«À´µÄÊýÖµ 101010*/
- t1.tm = atol(t1.buf);
- t1.hour = (unsigned char)(t1.tm / 10000); //10
- t1.min = (unsigned char)(t1.tm % 10000 / 100);
- t1.sec = (unsigned char)(t1.tm % 10000 % 100);
- status = 0;
- }
- if(status == 2){
- Connect();
- status = 0;
- }
- }
- }
- //ÍⲿÖжÏ
- interrupt [EXT_INT0] void Int0Server(void){
- status = 2;
- }
- //¶¨Ê±Æ÷1ÖжϷþÎñº¯Êý ¶¨Ê±1s
- interrupt [TIM1_OVF] void TimeServer(void){
- SREG = 0X80;
- TCNT1 = 0xe17b;
- if(++t1.sec == 60){
- t1.sec = 0;
- if(++t1.min == 60){
- t1.min = 0;
- if(++t1.hour == 24){
- t1.hour = 0;
- }
- }
- }
- }
- //´®¿ÚÖжϺ¯Êý£¬½ÓÊÜ´®¿ÚÊý¾Ý
- interrupt [USART_RXC] void UartServer(void){
- unsigned char tmp = UDR; //´®¿Ú´æ´¢Êý¾Ý
- static int i = 0, flag = 0;
- SREG = 0X00;
- if(tmp != 'r' && tmp != 'n' && i<7 && flag == 1){
- t1.buf[i] = tmp;
- i++;
- }else{
- /* if(tmp == 'r'){
- t1.buf[i] = '\0';
- }
- if(tmp == 'n'){
- i = 0;
- status = 1;
- LcdInit();
- flag = 0;
- }*/
- if( i == 7 ){
- t1.buf[i] = '\0';
- i = 0;
- status = 1;
- LcdInit();
- flag = 0;
- }
- }
- if(tmp == ':') flag = 1;
- SREG = 0X80;
- }
复制代码
lcd.c
- #include "lcd.h"
- #include
- void write_com(unsigned char com){
- RESETBIT(PORTA,1);
- PORTC = com;
- delay_ms(5);
- SETBIT(PORTA,0);
- delay_ms(5);
- RESETBIT(PORTA,0);
- }
- void write_data(unsigned char Data){
- SETBIT(PORTA,1);
- PORTC = Data;
- delay_ms(5);
- SETBIT(PORTA,0);
- delay_ms(5);
- RESETBIT(PORTA,0);
- }
- void DisChar(unsigned char Row, unsigned char Col, unsigned char Ch){
- switch(Row){
- case 1: write_com(0x80 + Col); break; //µÚÒ»ÐÐ
- case 2: write_com(0xc0 + Col); break; //µÚ¶þÐÐ
- }
- write_data(Ch);
- delay_ms(2);
- }
- void DisString(unsigned char Row, unsigned char Col, unsigned char *Str){
- unsigned char i = 0;
- for( i=0; Str[i]!='\0'; i++, Col++ ){
- switch(Row){
- case 1: write_com(0x80 + Col); break; //µÚÒ»ÐÐ
- case 2: write_com(0xc0 + Col); break; //µÚ¶þÐÐ
- }
- write_data(Str[i]);
- delay_ms(2);
- }
- }
- void LcdInit(void){
- RESETBIT(PORTA,0);
- write_com(0x38);
- write_com(0x0c);
- write_com(0x06);
- write_com(0x01);
- }
复制代码
lcd.h
- #ifndef _LCD_H_
- #define _LCD_H_
- #include
- #define SETBIT(A,B) A|=(1<
- #define RESETBIT(A,B) A&=~(1<
- void write_com(unsigned char);
- void write_data(unsigned char );
- void LcdInit(void);
- void DisChar(unsigned char , unsigned char , unsigned char );
- void DisString(unsigned char , unsigned char , unsigned char *);
- #endif
复制代码
mcu.c
- #include
- void PortInit(void){
- DDRA = 0XFF;
- PORTA = 0XFF;
- DDRC = 0XFF;
- PORTC = 0XFF;
- DDRD = 0X02;
- PORTD = 0XFF;
- }
- void T0Init(void){
- TCCR1B = 0X05; //1024·ÖƵ
- TCNT1 = 0xe17b; //ÑÓʱ1s
- }
- void UartInit(void){
- UCSRB = 0X00; //½ûÖ¹·¢ËÍÓë½ÓÊÕ
- UCSRA = 0X82; //±¶ËÙ
- UCSRC = 0X06; //8λÊý¾Ýλ
- UBRRL = 0X67; //²¨ÌØÂÊ9600
- UBRRH = 0X00;
- UCSRB = 0X98;
- }
- void Ex0Init(void){
- MCUCR |= 0x02; // INT0 ϽµÑØ´¥·¢
- GICR |= 0X40;
- }
- void McuInit(void){
- PortInit();
- T0Init();
- UartInit();
- Ex0Init();
- }
复制代码
mcu.h
- #ifndef _MCU_INIT_H_
- #define _MCU_INIT_H_
- void PortInit(void);
- void T0Init(void);
- void UartInit(void);
- void Ex0Init(void);
- void McuInit(void);
- #endif
复制代码
服务器代码:
- /*************************************
- 文件名: server.c
- linux 下socket网络编程简例 - 服务端程序
- 服务器端口设为 0x8888 (端口和地址可根据实际情况更改,或者使用参数传入)
- 服务器地址设为 192.168.1.104
- 作者:kikilizhm#163.com (将#换为@)
- */
-
- #include
- #include
- #include
- #include
- #include
- #include
- #include
-
- int main()
- {
- int sfp,nfp; /* 定义两个描述符 */
- struct sockaddr_in s_add,c_add;
- int sin_size;
- time_t nowtime;
- struct tm *timeinfo;
- int hour, min, sec;
- char buf[7];
- unsigned short portnum=8086; /* 服务端使用端口 */
-
- sfp = socket(AF_INET, SOCK_STREAM, 0);
- if(-1 == sfp)
- {
- printf("socket fail ! rn");
- return -1;
- }
- //printf("socket ok !rn");
-
- /* 填充服务器端口地址信息,以便下面使用此地址和端口监听 */
- bzero(&s_add,sizeof(struct sockaddr_in));
- s_add.sin_family=AF_INET;
- s_add.sin_addr.s_addr=htonl(INADDR_ANY); /* 这里地址使用全0,即所有 */
- s_add.sin_port=htons(portnum);
- /* 使用bind进行绑定端口 */
- if(-1 == bind(sfp,(struct sockaddr *)(&s_add), sizeof(struct sockaddr)))
- {
- printf("bind fail !rn");
- return -1;
- }
- //printf("bind ok !rn");
- /* 开始监听相应的端口 */
- if(-1 == listen(sfp,5))
- {
- printf("listen fail !rn");
- return -1;
- }
- //printf("listen okrn");
-
- while(1)
- {
- sin_size = sizeof(struct sockaddr_in);
- /* accept服务端使用函数,调用时即进入阻塞状态,等待用户进行连接,在没有客户端进行连接时,程序停止在此处,
- 不会看到后面的打印,当有客户端进行连接时,程序马上执行一次,然后再次循环到此处继续等待。
- 此处accept的第二个参数用于获取客户端的端口和地址信息。
- */
- nfp = accept(sfp, (struct sockaddr *)(&c_add), &sin_size);
- if(-1 == nfp)
- {
- printf("accept fail !rn");
- return -1;
- }
- //printf("accept ok!rnServer start get connect from %#x : %#xrn",ntohl(c_add.sin_addr.s_addr),ntohs(c_add.sin_port));
- printf("有人链接了!n");
- sleep(8);
- time( &nowtime );
- timeinfo = localtime( &nowtime );
- hour = timeinfo->tm_hour;
- min = timeinfo->tm_min;
- sec = timeinfo->tm_sec;
- sprintf(buf, "%d%d%d", hour, min, sec);
- /* 这里使用write向客户端发送信息,也可以尝试使用其他函数实现 */
- if(-1 == write(nfp,buf,sizeof(buf)))
- {
- printf("write fail!rn");
- return -1;
- }
- printf("write ok!rn");
- close(nfp);
-
- }
- close(sfp);
- return 0;
- }
复制代码
|