练习是制作一个程序,读取端口A的引脚2,3,4和5,并将这些引脚的状态复制到端口B上的输出引脚3,4,5和6,并创建一个函数打印LCD显示屏上激活的开关数量。我几乎理解了一切,但仍然有一些问题。我有点混淆pullups / pulldowns / high / low等...如果你能在这里给我一些光,那将是很好的!谢谢你。
#include <stdint.h>
#include <avr/io.h>
#include "lcd.h"
#include <stdio.h>
display myLCD;
count(unsigned char numbers){ //*1
uint8_t stand=0;
for (uint8_t i=3;i<=6;i++){
if(numbers&(1<<i))
stand+=1;
}
return(stand);
}
int main(void){
char text[20];
unsigned int numb;
lcd_init(&myLCD,&PORTD);
DDRA&=~((1<<PIN2)|(1<<PIN3)|(1<<PIN4)|(1<<PIN5)); //Setting PIN2, PIN3, PIN4 and PIN5 of DDRA as input
DDRB|=(1<<PIN3)|(1<<PIN4)|(1<<PIN5)|(1<<PIN6); //Setting PIN3, PIN4, PIN5 and PIN6 of DDRB as output
for (;;){
numb=PINA; //Gets the state of pins 2, 3, 4 and 5 of port A, shifts left, and copy on pins 3, 4, 5 and 6 on port B.
PORTB=((numb&((1<<PIN2)|(1<<PIN3)|(1<<PIN4)|(1<<PIN5)))<<1);
sprintf(text,"Aktive Switches:%i",count(~PORTB)); //*2
lcd_send_string(&myLCD,text,3,1);
}
return(0);
}
* 1:我理解功能从3到6(4个输出引脚)计数,如果发生了什么事(你认为&#34;数字&#34;是什么意思),它会计算它,但我不会通过计算激活的开关数来真正得到它们的含义。
* 2:我也不明白为什么我必须在计数(~portb)上否定(〜)端口b。
还有另一个程序,我有类似的问题。在这里,我只需将Timer1设置为在CTC模式下工作,并通过中断切换3个LED的状态。但我也无法理解为什么当我想打开LED时我必须否定端口!不能用上拉/下拉吗?
ISR(TIMER1_COMPA_vect){
time++;
if(time<10){
PORTC=~(1<<PIN0); //Red turned on
}
if(time==10){
PORTC=~((1<<PIN1)|(1<<PIN0)); //Red and yellow turned on
}
if(time==12){
PORTC=(1<<PIN0); //Red turned off
PORTC=(1<<PIN1); //Yellow turned off
PORTC=~(1<<PIN2); //Green turned on
}
if(time==22){
PORTC=(1<<PIN0); //Red turned off
PORTC=~(1<<PIN1); //Yellow turned on
PORTC=(1<<PIN2); //Red turned off
}
if(time==25){
PORTC=~(1<<PIN0); //Red turned on
time=0;
}
}
再次感谢你!