Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <math.h>
- #include <MsTimer2.h>
- #include <LedControl.h>
- int count = 0;
- int DIN = 12;
- int CS = 11;
- int CLK = 10;
- const float circle = 1.89; // 周长
- unsigned int timeCount = 0;
- byte L0[8] = {0x60,0x90,0x90,0x90,0x90,0x90,0x60,0x00};
- byte L1[8] = {0x20,0x60,0xa0,0x20,0x20,0x20,0xf0,0x00};
- byte L2[8] = {0x60,0x90,0x10,0x20,0x40,0x80,0xf0,0x00};
- byte L3[8] = {0x60,0x90,0x10,0x20,0x10,0x90,0x60,0x00};
- byte L4[8] = {0x20,0x60,0xa0,0xf0,0x20,0x20,0x20,0x00};
- byte L5[8] = {0xf0,0x80,0x80,0xe0,0x10,0x10,0xe0,0x00};
- byte L6[8] = {0x20,0x40,0x80,0xe0,0x90,0x90,0x60,0x00};
- byte L7[8] = {0xf0,0x90,0x10,0x20,0x40,0x40,0x40,0x00};
- byte L8[8] = {0x60,0x90,0x90,0x60,0x90,0x90,0x60,0x00};
- byte L9[8] = {0x60,0x90,0x90,0x70,0x20,0x40,0x80,0x00};
- byte R0[8] = {0x06,0x09,0x09,0x09,0x09,0x09,0x06,0x00};
- byte R1[8] = {0x02,0x06,0x0a,0x02,0x02,0x02,0x0f,0x00};
- byte R2[8] = {0x06,0x09,0x01,0x02,0x04,0x08,0x0f,0x00};
- byte R3[8] = {0x06,0x09,0x01,0x02,0x01,0x09,0x06,0x00};
- byte R4[8] = {0x02,0x06,0x0a,0x0f,0x02,0x02,0x02,0x00};
- byte R5[8] = {0x0f,0x08,0x08,0x0e,0x01,0x01,0x0e,0x00};
- byte R6[8] = {0x02,0x04,0x08,0x0e,0x09,0x09,0x06,0x00};
- byte R7[8] = {0x0f,0x09,0x01,0x02,0x04,0x04,0x04,0x00};
- byte R8[8] = {0x06,0x09,0x09,0x06,0x09,0x09,0x06,0x00};
- byte R9[8] = {0x06,0x09,0x09,0x07,0x02,0x04,0x08,0x00};
- byte STOP[8] = {0x18,0x3c,0x3c,0x18,0x18,0x00,0x18,0x18}; // 输出感叹号“!”
- LedControl lc=LedControl(DIN,CLK,CS,4);
- void setup() {
- attachInterrupt(0, blink, LOW);
- Serial.begin(9600); // 串口通信波特率
- lc.shutdown(0,false); //启动时,MAX7219处于省电模式
- lc.setIntensity(0,3); //将亮度设置为最大值
- lc.clearDisplay(0); //清除显示
- MsTimer2::set(1, timeTo); // 设定时间间隔
- MsTimer2::start(); // 启动定时器
- }
- void loop() {
- }
- void timeTo() {
- // 计时
- timeCount += 1;
- if (timeCount > 4000) {
- printMerge(L0,R0);
- }
- }
- void blink() {
- // 阈值判断
- if (timeCount > 100){
- calSpeed();
- }
- timeCount = 0;
- }
- // 计算速度
- void calSpeed() {
- float speed = circle / timeCount * 3600 + 1; // 3600为1000ms*3.6km/h, +1 为延迟补偿
- if (speed > 50.0) {
- printByte(STOP);
- } else {
- // 显示换算
- int L_tmp = speed / 10;
- int R_tmp = int(fmod(speed, 10)); // 浮点数取余
- byte L[8];
- byte R[8];
- switch (L_tmp) {
- case 0: memcpy(L, L0, 8); break;
- case 1: memcpy(L, L1, 8); break;
- case 2: memcpy(L, L2, 8); break;
- case 3: memcpy(L, L3, 8); break;
- case 4: memcpy(L, L4, 8); break;
- case 5: memcpy(L, L5, 8); break;
- case 6: memcpy(L, L6, 8); break;
- case 7: memcpy(L, L7, 8); break;
- case 8: memcpy(L, L8, 8); break;
- case 9: memcpy(L, L9, 8); break;
- }
- switch (R_tmp) {
- case 0: memcpy(R, R0, 8); break;
- case 1: memcpy(R, R1, 8); break;
- case 2: memcpy(R, R2, 8); break;
- case 3: memcpy(R, R3, 8); break;
- case 4: memcpy(R, R4, 8); break;
- case 5: memcpy(R, R5, 8); break;
- case 6: memcpy(R, R6, 8); break;
- case 7: memcpy(R, R7, 8); break;
- case 8: memcpy(R, R8, 8); break;
- case 9: memcpy(R, R9, 8); break;
- }
- printMerge(L, R);
- }
- }
- // 单字符输出
- void printByte(byte character []) {
- for (int i = 0; i < 8; i++) {
- lc.setRow(0, i, character[i]);
- }
- }
- // 双字符输出
- void printMerge(byte x [], byte y []) {
- byte c[8];
- for (int i = 0; i < 8; i++) {
- c[i] = x[i] + y[i];
- }
- printByte(c);
- }
- // 自行车车轮周长3.14 * 600 =1890mm
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement