第3步编写代码
在C源程序中,14400(14400秒相当于4小时)指的是自动开灯的时间,你可以修改这个时间。
#include <avr/io.h> #include "global.h" #include <util/delay.h> int main(){ DDRA = 0x04; PORTA = 0x00; ADCSR |= (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0); // Set ADC prescalar to 128 - 125KHz sample rate @ 16MHz ADMUX |= (1 << ADLAR); // Left adjust ADC result to allow easy 8 bit reading ADMUX |= (1 << MUX0); ADCSR |= (1 << ADFR); // Set ADC to Free-Running Mode ADCSR |= (1 << ADEN); // Enable ADC ADCSR |= (1 << ADSC); // Start A2D Conversions unsigned char sensor = 0; unsigned short int sekund = 14400; while(1){ ADCSR |= (1 << ADSC); // Start A2D Conversions sensor = ADCH; //PORTB = sensor; if(sensor > 160 && sekund){ PORTA=0x04; sekund--; } else { PORTA=0x00; if(sensor <= 160){ sekund = 14400; } } _delay_ms(1000); } }
:100000000BC024C023C022C021C020C01FC01EC0FE :100010001DC01CC01BC01AC011241FBECFEDCDBF18 :1000200010E0A0E6B0E0E4EAF0E003C0C895319645 :100030000D92A036B107D1F710E0A0E6B0E001C004 :100040001D92A036B107E1F702D02AC0D9CF84E0D3 :100050008ABB1BBA86B1876086B93D9A389A359AB1 :10006000379A369A20E438E364E049E150E0369A62 :1000700085B1813A38F02115310521F06BBB215053 :10008000304005C01BBA813A10F420E438E380E127 :1000900097E2E42FF52F3197F1F70197D1F7E7CFEA :0400A000F894FFCF02 :00000001FF
评 论
选择昵称后请轻按空格键
请勿进行人身攻击,谩骂以及任何违法国家相关法律法规的言论。