void main(void) { unsigned char i, data; main_init(); hc595_init(); // Blank output hc595_write_byte(0x00); hc595_write_byte(0xff); hc595_latch(); hc595_oe(); __enable_interrupt(); i = 1; while(1) { if (switch0_state) { hh++; if (hh > 23) hh = 0; switch0_state = 0; time_set = 1; } if (switch1_state) { mm++; if (mm > 59) mm = 0; ss = 0; switch1_state = 0; time_set = 1; } // Continuously scan multiplexed display // Assert the current digit's anode hc595_write_byte(~i); // Select correct data for current digit switch(i) { case 1: data = digits[hh / 10]; break; case 2: data = digits[hh % 10]; // Toggle the middle decimal point as a seconds heartbeat if (ss % 2) data |= 0x80; break; case 4: data = digits[mm / 10]; break; case 8: data = digits[mm % 10]; break; } // flash if time not set if ((!time_set) && (ss%2)) { data = 0; } hc595_write_byte(data); hc595_latch(); // move on to the next digit i <<= 1; if (i>8) i = 1; } }
int main() { hc595_init(); initTimer0(); int8_t shiftCounter = 0x00; int16_t curShift, curMaxShift; int16_t charCol, row; int16_t col; //revspace: 47 cols uint8_t textLength = 190;//77 + 113+;//198; //test length // strlen(s) * 6 //all output DDRD = 0xFF; DDRA = 0xFF; char text[] = "nee"; textLength = strlen(text);//10 * 5 + 9; int8_t j, i; while(1) { if(shiftIn > 80 + textLength) shiftIn = 0; curMaxShift = shiftIn; //scan the rows for(row = 7; row > 0; row--) { charCol = textLength; //word length PORTA = row; hc595_latchLow(); // all the columns + the length of the text for(col = 80 + textLength; col >= 0; ) { if(80 + textLength - curMaxShift > col && charCol >= 0) { for(i = strlen(text) - 1; i >= 0; i--) { for(j = 4; j >= 0; j--) { if(~chars[text[i] - 'a'][j] & (1 << row)) { hc595_dataHigh(); } else { hc595_dataLow(); } charCol--; hc595_clk(); } hc595_dataHigh(); hc595_clk(); } } else { hc595_dataHigh(); } col--; hc595_clk(); } hc595_latchHigh(); // hc595_latchLow(); // for(col = 80 + textLength; col >= 0; col--){ // if( textLength + 80 - curMaxShift > col && charCol >= 0) { // if(~text[charCol] & (1 << row)) // hc595_dataHigh(); // else // hc595_dataLow(); // // [>if(~orange[charCol] & (1 << row))<] // // [>hc595_dataRedHigh();<] // // [>else<] // // [>hc595_dataRedLow();<] // charCol -= 1; // } // else { // hc595_dataHigh(); // } // hc595_clk(); // } // hc595_latchHigh(); } } }
int main(void) { u8 readstatus,firstread=0; u8 humi_set_temp=humi_set; u8 temp_set_temp=temp_set; //main1(); /* USART1 config 115200 8-N-1 */ // USART1_Config(); // // printf("\r\n this is a yhx firmware \r\n"); /* 配置SysTick 为1us中断一次 */ SysTick_Init(); // TIM3_Int_Init(100,7199); DelayInit(); // delay_init(72); /*按键输入初始化*/ GPIO_INPUT_INIT(); /*输出初始化*/ IO_OUTPUT_Config(); /* I2C 外设初(AT24C02)始化 */ I2C_EE_Init(); /*当按下-键时,程序会执行默认的IP192.168.3.248,不会从eeprom读取数据*/ if( Key_Scan(KEYPORT,KEY_SUB,1) != KEY_ON ) I2C_EE_Restore(); if(EE_FLAG!=i2c_eeprom_data[0]) { //初始温湿度设定及裕量 i2c_eeprom_data[0]=EE_FLAG; i2c_eeprom_data[1]=temp_set; i2c_eeprom_data[2]=humi_set; i2c_eeprom_data[3]=temp_distance; i2c_eeprom_data[4]=humi_distance; //初始化net //ip i2c_eeprom_data[8]=ipaddress[0]; i2c_eeprom_data[9]=ipaddress[1]; i2c_eeprom_data[10]=ipaddress[2]; i2c_eeprom_data[11]=ipaddress[3]; //mask i2c_eeprom_data[12]=maskaddress[0]; i2c_eeprom_data[13]=maskaddress[1]; i2c_eeprom_data[14]=maskaddress[2]; i2c_eeprom_data[15]=maskaddress[3]; //gate i2c_eeprom_data[16]=gateaddress[0]; i2c_eeprom_data[17]=gateaddress[1]; i2c_eeprom_data[18]=gateaddress[2]; i2c_eeprom_data[19]=gateaddress[3]; //mac i2c_eeprom_data[24]=mymac[0]; i2c_eeprom_data[25]=mymac[1]; i2c_eeprom_data[26]=mymac[2]; i2c_eeprom_data[27]=mymac[3]; i2c_eeprom_data[28]=mymac[4]; i2c_eeprom_data[29]=mymac[5]; I2C_EE_WaitOperationIsCompleted(); I2C_EE_WriteBuffer(i2c_eeprom_data, 0, 8); //网络 I2C_EE_WaitOperationIsCompleted(); I2C_EE_WriteBuffer(i2c_eeprom_data+8, 8, 8); I2C_EE_WaitOperationIsCompleted(); I2C_EE_WriteBuffer(i2c_eeprom_data+16, 16, 8); I2C_EE_WaitOperationIsCompleted(); I2C_EE_WriteBuffer(i2c_eeprom_data+24, 24, 8); } //中断开启,配置net管脚和传感器data管脚中断 // NVIC_Configuration(); /*SHT10-模拟I2C读取温湿度,初始化*/ // Sht_GPIO_Config(); // s_connectionreset(); // sht10_read(); /*初始化DTT11的引脚*/ DHT11_GPIO_Config(); SPI1_Init(); //InitNet(); /* 初始化网络设备以及UIP协议栈,配置IP地址 */ //数码管初始化 hc595_init(); /* 配置SysTick 为10ms中断一次 */ // SysTick_Init(); TimingInput=0;//输入节拍20ms IWDG_Init(4,625); //与分频数为64,重载值为625,溢出时间为1s while(1) { /*温湿度测量,massureflag请求测量标志*/ if(massureflag) { //数据伪造1213,温度控制用的是真实的,显示和上传的数据用的是渐变的伪造数据 if( (readstatus=Read_DHT11(&DHT11_Data))==SUCCESS) { if(firstread==0) { temp_val.f=DHT11_Data.temp_int; firstread=1; } else { if(DHT11_Data.temp_int>(int)temp_val.f) temp_val.f=temp_val.f+0.5; else if(DHT11_Data.temp_int<(int)temp_val.f) temp_val.f=temp_val.f-0.5; } humi_val.f=DHT11_Data.humi_int; sensor_error=0; massureflag=0; time18ms_out=0; if(set_wait>1000)//初始化已过 if((temp_val.f>-10)&&(temp_val.f<100)&&(humi_val.f>=0/*)&&(humi_val.f<100*/)&&poweron)//传感器没坏才输出 automation(); }else if(readstatus!=NOTREADY) { sensor_error++; } } if(sensor_error>5)//5次都没有采到温湿度 { temp_val.f=humi_val.f=-100; } //printf("温度:%.1f\t湿度%.1f\r\n",temp_val.f,humi_val.f); //根据检测到的温湿度控制led和继电器,刚开机3s的时候尚未检测到温湿度,此时最好不要有输出,在while之前添加sht10_read()吧,进来之前就读一次数据 if((0==poweron)||((time_s_remainder==0)&&(active!='a'))) { WARM(0); WARM_LED(0); COLD(0); COLD_LED(0); WET(0); WET_LED(0); ALWAYS_WET_LED(0); BEEP_SCREAM(0); work_state=0; } if(network_state==0) { //下面的代码是按键检测,用来设定温湿度设定值的 if(TimingInput)//输入检测20ms一次 { TimingInput=0; key_input_last=key_input; GPIO_INPUT_SCAN(); if(key_input==key_input_last)//检测是否一直按着同一个按键 press_same_key_flag=1;//一直按着同一个按键 else press_same_key_flag=0; //蜂鸣器响应 if((key_input)&&(beeptime<300)) { //蜂鸣器响 BEEP_SCREAM(1); }else { if(key_input==0) beeptime=0; //蜂鸣器关闭 BEEP_SCREAM(0); } //是否进入设定状态,闪烁。 if(0==showdigital)//之前就是闪烁状态,temp_set_temp数据要保留 { temp_set_temp=temp_set; humi_set_temp=humi_set; } if((0==press_same_key_flag)&&(0!=key_input))//有按键按下但不是同一个按键 { //输入处理 switch (key_input) { case 1://set if(set_wait<=1000) set_start_flag=1; showdigital++;//第showdigital位闪烁 if(showdigital>4) showdigital=1; break; case 2://save showdigital=0; set_start_flag=0;//初始化完成标志 set_wait=1001;//设置等待10s结束 //将数据保存到eeprom,温湿度设定值 temp_set=temp_set_temp; humi_set=humi_set_temp; i2c_eeprom_data[1]=temp_set; i2c_eeprom_data[2]=humi_set; //写入eeprom i2c_eeprom_data[0]=EE_FLAG; I2C_EE_WaitOperationIsCompleted(); I2C_EE_WriteBuffer(i2c_eeprom_data, 0, 5); break; case 4://+ //第showdigital位+ switch(showdigital) { case 1: temp_set_temp=(temp_set_temp/10+1)%10*10+temp_set_temp%10; break; case 2: temp_set_temp=temp_set_temp/10*10+(temp_set_temp%10+1)%10; break; case 3: humi_set_temp=(humi_set_temp/10+1)%10*10+humi_set_temp%10; break; case 4: humi_set_temp=humi_set_temp/10*10+(humi_set_temp%10+1)%10; break; default:; } break; case 8://- //第showdigital位+ switch(showdigital) { case 1: temp_set_temp=(temp_set_temp<10)?90+temp_set_temp:(temp_set_temp/10-1)%10*10+temp_set_temp%10; break; case 2: temp_set_temp=(temp_set_temp%10==0)?temp_set_temp+9:temp_set_temp-1; break; case 3: humi_set_temp=(humi_set_temp<10)?90+humi_set_temp:(humi_set_temp/10-1)%10*10+humi_set_temp%10; break; case 4: humi_set_temp=(humi_set_temp%10==0)?humi_set_temp+9:humi_set_temp-1; break; default:; } break; default:; } } } }else {//本地状态显示 humi_set_temp=humi_set; temp_set_temp=temp_set; key_input=0; showdigital=0; BEEP_SCREAM(0); beeptime=0; } //试用期写入 if((active!='a')&&(remainder_write>300)) { remainder_write=0; //写入eeprom //if(time_s_remainder>0) { i2c_eeprom_data[32]=(u8)time_s_remainder; i2c_eeprom_data[33]=(u8)(time_s_remainder>>8); i2c_eeprom_data[34]=(u8)(time_s_remainder>>16); i2c_eeprom_data[35]=(u8)(time_s_remainder>>24); I2C_EE_WaitOperationIsCompleted(); I2C_EE_WriteBuffer(i2c_eeprom_data+32, 32, 4); } } //sht10_j: //数码管输出 digitaldata[0]=digitaldata[1]=digitaldata[2]=digitaldata[3]=digitaldata[4]=digitaldata[5]=12;//12 if((active=='a')||(time_s_remainder>0))//激活态20150421 { //温度 if((temp_val.f<0)&&(temp_val.f>-10))//负值 { digitaldata[0]=10;//- digitaldata[1]=(u8)(-temp_val.f); digitaldata[2]=(u8)(-temp_val.f*10)%10; }else if((temp_val.f>=0)&&(temp_val.f<100))//正数 { digitaldata[0]=((u8)temp_val.f)/10; digitaldata[1]=((u8)temp_val.f)%10;//. digitaldata[2]=(u8)(((u16)(temp_val.f*10))%10); } //湿度 if(humi_val.f>=0)//((humi_val.f>=0)&&(humi_val.f<100)) { if(humi_val.f>=100) { temp_f=humi_val.f;//20150712 add temp xzk humi_val.f=99.9; } digitaldata[3]=((u8)humi_val.f)/10; digitaldata[4]=((u8)humi_val.f)%10;//. digitaldata[5]=(u8)(((u16)(humi_val.f*10))%10); if(humi_val.f>=99.9) humi_val.f=temp_f; } }else //试用期已经到了,显示off 20150421 { digitaldata[0]=0; digitaldata[3]=0; BEEP_SCREAM(1); } //设定温度值 digitaldata[6]=temp_set_temp/10; digitaldata[7]=temp_set_temp%10; //湿度设定 digitaldata[8]=humi_set_temp/10; digitaldata[9]=humi_set_temp%10; if((blickflag)&&(network_state==0))//可以闪烁 switch(showdigital) {//showdigital=0//低位到高位是否显示6 7 8 9,0都显示,1:9不显示;2:8不显示;3:7不显示;4:6不显示 case 1: digitaldata[6]=11; break; case 2: digitaldata[7]=11; break; case 3: digitaldata[8]=11; break; case 4: digitaldata[9]=11; break; default:; } hc595_display(digitaldata); //就地状态或网络状态PC3 if( Key_Scan(GPIOC,GPIO_Pin_3,0) == KEY_ON ) { /*按键有效*/ network_state=1; }else//就地 { network_state=0; //poweron=1; //0716 //启动开关PA12检测 20150719 if( Key_Scan(GPIOA,GPIO_Pin_12,1) == KEY_ON ) { poweron=1; }else { poweron=0; } } /* 下面是网络检测的程序,处理uip事件,必须插入到用户程序的循环体中 */ UipPro(); //中断触发读取网络接收缓存 eth_poll(); //定时查询TCP及UDP连接收发状态 ARP表更新, 并响应 IWDG_Feed();//喂狗 if(request_initnet>250)//等待2s初始化网络 { InitNet(); request_initnet=0; } }
int main(int argc, char *argv[]) { elf_firmware_t f; const char * fname = "atmega168_timer_64led.axf"; //char path[256]; // sprintf(path, "%s/%s", dirname(argv[0]), fname); //printf("Firmware pathname is %s\n", path); elf_read_firmware(fname, &f); printf("firmware %s f=%d mmcu=%s\n", fname, (int)f.frequency, f.mmcu); avr = avr_make_mcu_by_name(f.mmcu); if (!avr) { fprintf(stderr, "%s: AVR '%s' now known\n", argv[0], f.mmcu); exit(1); } avr_init(avr); avr_load_firmware(avr, &f); // // initialize our 'peripherals' // hc595_init(avr, &shifter); button_init(avr, &button[B_START], "button.start"); avr_connect_irq( button[B_START].irq + IRQ_BUTTON_OUT, avr_io_getirq(avr, AVR_IOCTL_IOPORT_GETIRQ('C'), 0)); button_init(avr, &button[B_STOP], "button.stop"); avr_connect_irq( button[B_STOP].irq + IRQ_BUTTON_OUT, avr_io_getirq(avr, AVR_IOCTL_IOPORT_GETIRQ('B'), 1)); button_init(avr, &button[B_RESET], "button.reset"); avr_connect_irq( button[B_RESET].irq + IRQ_BUTTON_OUT, avr_io_getirq(avr, AVR_IOCTL_IOPORT_GETIRQ('B'), 0)); // connects the fake 74HC595 array to the pins avr_irq_t * i_mosi = avr_io_getirq(avr, AVR_IOCTL_SPI_GETIRQ(0), SPI_IRQ_OUTPUT), * i_reset = avr_io_getirq(avr, AVR_IOCTL_IOPORT_GETIRQ('D'), 4), * i_latch = avr_io_getirq(avr, AVR_IOCTL_IOPORT_GETIRQ('D'), 7); avr_connect_irq(i_mosi, shifter.irq + IRQ_HC595_SPI_BYTE_IN); avr_connect_irq(i_reset, shifter.irq + IRQ_HC595_IN_RESET); avr_connect_irq(i_latch, shifter.irq + IRQ_HC595_IN_LATCH); avr_irq_t * i_pwm = avr_io_getirq(avr, AVR_IOCTL_TIMER_GETIRQ('0'), TIMER_IRQ_OUT_PWM0); avr_irq_register_notify( i_pwm, pwm_changed_hook, NULL); avr_irq_register_notify( shifter.irq + IRQ_HC595_OUT, hc595_changed_hook, NULL); // even if not setup at startup, activate gdb if crashing avr->gdb_port = 1234; if (0) { //avr->state = cpu_Stopped; avr_gdb_init(avr); } /* * VCD file initialization * * This will allow you to create a "wave" file and display it in gtkwave * Pressing "r" and "s" during the demo will start and stop recording * the pin changes */ avr_vcd_init(avr, "gtkwave_output.vcd", &vcd_file, 10000 /* usec */); avr_vcd_add_signal(&vcd_file, avr_get_interrupt_irq(avr, 7), 1 /* bit */ , "TIMER2_COMPA" ); avr_vcd_add_signal(&vcd_file, avr_get_interrupt_irq(avr, 17), 1 /* bit */ , "SPI_INT" ); avr_vcd_add_signal(&vcd_file, i_mosi, 8 /* bits */ , "MOSI" ); avr_vcd_add_signal(&vcd_file, i_reset, 1 /* bit */ , "595_RESET" ); avr_vcd_add_signal(&vcd_file, i_latch, 1 /* bit */ , "595_LATCH" ); avr_vcd_add_signal(&vcd_file, button[B_START].irq + IRQ_BUTTON_OUT, 1 /* bits */ , "start" ); avr_vcd_add_signal(&vcd_file, button[B_STOP].irq + IRQ_BUTTON_OUT, 1 /* bits */ , "stop" ); avr_vcd_add_signal(&vcd_file, button[B_RESET].irq + IRQ_BUTTON_OUT, 1 /* bits */ , "reset" ); avr_vcd_add_signal(&vcd_file, shifter.irq + IRQ_HC595_OUT, 32 /* bits */ , "HC595" ); avr_vcd_add_signal(&vcd_file, i_pwm, 8 /* bits */ , "PWM" ); // 'raise' it, it's a "pullup" avr_raise_irq(button[B_START].irq + IRQ_BUTTON_OUT, 1); avr_raise_irq(button[B_STOP].irq + IRQ_BUTTON_OUT, 1); avr_raise_irq(button[B_RESET].irq + IRQ_BUTTON_OUT, 1); printf( "Demo : This is a real world firmware, a 'stopwatch'\n" " timer that can count up to 99 days. It features a PWM control of the\n" " brightness, blinks the dots, displays the number of days spent and so on.\n\n" " Press '0' to press the 'start' button\n" " Press '1' to press the 'stop' button\n" " Press '2' to press the 'reset' button\n" " Press 'q' to quit\n\n" " Press 'r' to start recording a 'wave' file - with a LOT of data\n" " Press 's' to stop recording\n" " + Make sure to watch the brightness dim once you stop the timer\n\n" ); /* * OpenGL init, can be ignored */ glutInit(&argc, argv); /* initialize GLUT system */ int w = 22, h = 8; glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); glutInitWindowSize(w * pixsize, h * pixsize); /* width=400pixels height=500pixels */ window = glutCreateWindow("Press 0, 1, 2 or q"); /* create window */ // Set up projection matrix glMatrixMode(GL_PROJECTION); // Select projection matrix glLoadIdentity(); // Start with an identity matrix glOrtho(0, w * pixsize, 0, h * pixsize, 0, 10); glScalef(1,-1,1); glTranslatef(0, -1 * h * pixsize, 0); glutDisplayFunc(displayCB); /* set window's display callback */ glutKeyboardFunc(keyCB); /* set window's key callback */ glutTimerFunc(1000 / 24, timerCB, 0); // the AVR run on it's own thread. it even allows for debugging! pthread_t run; pthread_create(&run, NULL, avr_run_thread, NULL); glutMainLoop(); }