1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
| #include <WiFi.h> #include <time.h> #include "NTPClient.h" #include <Ds1302.h>
const char* ssid = "ssid"; const char* password = "password";
int CLK = 7; int CS = 6; int DIN = 4; int sw1=18; int sw2=19; int sw3=10;
int sec; int n=10; #define battery 0 WiFiUDP ntpUDP; NTPClient timeClient(ntpUDP, "ntp1.aliyun.com", 60*60*8, 30*60*1000); void smgdispaly() { if(sec!=timeClient.getSeconds()){ int h1,h2,m1,m2,s1,s2; h1=timeClient.getHours()/10; h2=timeClient.getHours()%10; m1=timeClient.getMinutes()/10; m2=timeClient.getMinutes()%10; s1=timeClient.getSeconds()/10; s2=timeClient.getSeconds()%10; Write_Max7219(0x0f, 0x00); Write_Max7219(1,h1); Write_Max7219(2,h2); Write_Max7219(4,m1); Write_Max7219(5,m2); Write_Max7219(7,s1); Write_Max7219(8,s2); sec=timeClient.getSeconds(); } }
void batterydisplay(){ float analog_value = 0; analog_value = analogRead(battery); delay(1000); Serial.printf("Current Reading on Pin(%d)=%f\n",battery,analog_value*4.2/2910); }
void switchkey(){ if(digitalRead(sw2)==LOW){ delay(10); if(digitalRead(sw2)==LOW){ Serial.println(n); Write_Max7219(0x09, 0xff); Write_Max7219(0x0a, n++); if(n>=15) n=15; } } if(digitalRead(sw3)==LOW){ delay(10); if(digitalRead(sw3)==LOW){ Serial.println(n); Write_Max7219(0x09, 0xff); Write_Max7219(0x0a, n--); if(n<=0) n=0; } } } void setup() { Serial.begin(115200); pinMode(CLK,OUTPUT); pinMode(CS,OUTPUT); pinMode(DIN,OUTPUT); pinMode(battery,INPUT); pinMode(sw1,INPUT_PULLUP); pinMode(sw2,INPUT_PULLUP); pinMode(sw3,INPUT_PULLUP); Init_MAX7219(); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); timeClient.begin(); sec=timeClient.getSeconds(); Serial.println(timeClient.getFormattedTime()); }
void loop() { timeClient.update(); smgdispaly(); switchkey(); delay(50); } void Delay_xms(unsigned int x) { unsigned int i,j; for(i=0;i<x;i++) for(j=0;j<112;j++); }
void Write_Max7219_byte(unsigned char DATA) { unsigned char i; digitalWrite(CS,LOW); for(i=8;i>=1;i--) { digitalWrite(CLK,LOW); if(DATA&0X80) digitalWrite(DIN,HIGH); else digitalWrite(DIN,LOW); DATA<<=1; digitalWrite(CLK,HIGH); } }
void Write_Max7219(unsigned char address,unsigned char dat) { digitalWrite(CS,LOW); Write_Max7219_byte(address); Write_Max7219_byte(dat); digitalWrite(CS,HIGH); }
void Init_MAX7219(void) { Write_Max7219(0x09, 0xff); Write_Max7219(0x0a, 15); Write_Max7219(0x0b, 0x07); Write_Max7219(0x0c, 0x01); Write_Max7219(1,0xa); Write_Max7219(2,0xa); Write_Max7219(3,0xa); Write_Max7219(4,0xa); Write_Max7219(5,0xa); Write_Max7219(6,0xa); Write_Max7219(7,0xa); Write_Max7219(8,0xa); delay(100); }
|