用AVR实现网络调时
时间:10-02
整理:3721RD
点击:
单片机:ATMEGA16A-PU,WIFI模块:ESP8266,LCD1602液晶 实现网络调时,IDE是用CVAVR;后端是在Linux平台上开发的!,后端是从网上复制下来的,稍作些修改!
还有许多不完整的地方!希望大神们给出建议!
main.c
lcd.h
mcu.c
mcu.h
服务器代码:
还有许多不完整的地方!希望大神们给出建议!
main.c
- #include "lcd.h"
- #include "mcu.h"
- #include <delay.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- /******************±?á??¨ò? start*****************************/
- typedef struct Time{
- unsigned char sec;
- unsigned char min;
- unsigned char hour;
- unsigned char buf[8]; //′??úêy?Y′?′¢
- long int tm;
- } Time;
- Time t1 = {0, 0, 0, 0, 0}; //3?ê??ˉ?á11ì?
- /*
- * status ±íê???ê?×′ì?
- * 1£o×?·?′??óêüíê±?
- * 2£oWiFi?aê?·¢?í
- */
- static int status = 0; //×′ì?
- /******************±?á??¨ò? end*****************************/
- /******************oˉêy?¨ò? 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; //?D??1?±?
- printf("AT+RST\n"); //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\",8086\n");//á′?ó·t???÷
- //delay_ms(500);
- delay_ms(500); //?óê±5s
- SREG = 0X80; //?D???a??
- }
- /******************oˉêy?¨ò? end*****************************/
- void main(void){
- delay_ms(500); //μè′yμ??′?è?¨
- McuInit();
- TIMSK = 0X04; //?aê???ê±
- LcdInit();
- SREG = 0X80; //è????D??
- while(1){
- DisString(1, 3, "The time is");
- if(status == 0){
- DisPlay();
- }
- if(status == 1){
- /* ′|àí′??ú′?à′μ?êy?μ 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;
- }
- }
- }
- //ía2??D??
- interrupt [EXT_INT0] void Int0Server(void){
- status = 2;
- }
- //?¨ê±?÷1?D??·t??oˉêy ?¨ê±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;
- }
- }
- }
- }
- //′??ú?D??oˉêy£??óêü′??úêy?Y
- interrupt [USART_RXC] void UartServer(void){
- unsigned char tmp = UDR; //′??ú′?′¢êy?Y
- 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;
- }
- #include "lcd.h"
- #include <delay.h>
- 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; //μúò?DD
- case 2: write_com(0xc0 + Col); break; //μú?tDD
- }
- 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; //μúò?DD
- case 2: write_com(0xc0 + Col); break; //μú?tDD
- }
- 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 <mega16.h>
- #define SETBIT(A,B) A|=(1<<B)
- #define RESETBIT(A,B) A&=~(1<<B)
- 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 <mega16.h>
- 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; //???1·¢?íó??óê?
- UCSRA = 0X82; //±??ù
- UCSRC = 0X06; //8??êy?Y??
- UBRRL = 0X67; //2¨ì??ê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 <stdlib.h>
- #include <sys/types.h>
- #include <stdio.h>
- #include <time.h>
- #include <sys/socket.h>
- #include <cygwin/in.h>
- #include <string.h>
-
- 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 ! \r\n");
- return -1;
- }
- //printf("socket ok !\r\n");
-
- /* 填充服务器端口地址信息,以便下面使用此地址和端口监听 */
- 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 !\r\n");
- return -1;
- }
- //printf("bind ok !\r\n");
- /* 开始监听相应的端口 */
- if(-1 == listen(sfp,5))
- {
- printf("listen fail !\r\n");
- return -1;
- }
- //printf("listen ok\r\n");
-
- 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 !\r\n");
- return -1;
- }
- //printf("accept ok!\r\nServer start get connect from %#x : %#x\r\n",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!\r\n");
- return -1;
- }
- printf("write ok!\r\n");
- close(nfp);
-
- }
- close(sfp);
- return 0;
- }