【发布时间】:2015-02-22 14:48:55
【问题描述】:
你好朋友,我不知道 switch-case 和 if,else 语句对我都不起作用我想在某些特定数据之前在我的“开关块”中的 PORTA 寄存器中提供一些数据时向 PORTB 和 PORTD 提供一些数据我使用 PINA 而不是 PORTA,但它仍然无法正常工作,但是当我开始调试并通过给出 PORTA=0b00001110 提供一些数据时,它很容易给出值 PORTB=0b00000010 .... 请帮忙..
/*
* robotic_arm.c
*
* Created: 2/3/2015 10:39:25 AM
* Author: Shrikant Vaishnav
*/
#define F_CPU 1600000000UL
#include <avr/io.h>
#include <util/delay.h>
int main(void)
{ DDRA=0x00;//make PORTA as input
DDRB=0xFF;//make PORTB as output
DDRD=0XFF;//make PORTD as output
while(1)
{
switch(PORTA)
{
//First Three conditions for Robotic ARMs
case 0b00001110:
{
PORTB=0b00000010;
_delay_ms(50);
break;
}
case 0b00001101:
{
PORTB=0b00001000 ;
_delay_ms(50);
break;
}
case 0b00001011:
{
PORTB=0b00100000 ;
_delay_ms(50);
break;
}
//Condition for Direction Change of Motors of Robotic Arms
case 0b00000110:
{
PORTB=0b00000001;
_delay_ms(50);
break;
}
case 0b00000101:
{
PORTB=0b00000100;
_delay_ms(50);
break;
}
case 0b00000011:
{
PORTB=0b00100000;
_delay_ms(50);
break;
}
//Now Driving Robotic Car
case 0b00000010:
{
PORTD=0b00000010;
_delay_ms(50);
break;
}
case 0b00000001:
{
PORTD=0b00000001;
_delay_ms(50);
break;
}
default:
{
PORTB=0b00000000; //0ff motors when no signal sent
PORTD=0b00000000; //OFF DRIVING CAR
_delay_ms(50);
break;
}
}
}
return 0;
}
【问题讨论】:
-
可能你的外部开关不正确,或者给出一个简短的瞬态值。在后一种情况下,可能会调用
default语句。作为一个建议,您为什么不尝试将 PORTA 输入连续写入 PORTB 输出,然后看看会发生什么。顺便说一句,case 0b00001101: { PORTA=0b00001000; ...}声明中的你真的是想写信给PORTA吗? -
您可能不打算将时钟频率定义为 1.6GHz? _delay_ms() 需要正确设置 F_CPU。
标签: microcontroller avr atmega atmelstudio winavr