Бодаюсь с Atmega128
(«Телесистемы»: Конференция «Микроконтроллеры и их применение»)

миниатюрный аудио-видеорекордер mAVR

Отправлено НАЧАЛьник 25 июля 2005 г. 09:52

Написал монитор для работы с RS-232. Не могу достучаться до процессора. Подскажите, где я напортачил. Использовал IAR и AVR Studio 4.


Файл main.hpp

#include // Declares the internal register addresses for ATmega128
#include // Intrinsics for iccAVR
#include // Input/output
#include
#include
#include

typedef signed long int slint; // Типы
typedef unsigned int uint;
typedef unsigned char uchar;

#define bit(n) (1 << (n)) // Макросы
#define setbit(p,n) (p|=bit(n))
#define clrbit(p,n) (p&=~bit(n))
#define invbit(p,n) (p=p^bit(n))
#define tstbit(p,n) (p&bit(n))

#define RXB8 1 // Для USART'ов
#define TXB8 0
#define UPE 2
#define OVR 3
#define FE 4
#define UDRE 5
#define RXC 7
#define TXC 6
#define FRAMING_ERROR (1<#define PARITY_ERROR (1<#define DATA_OVERRUN (1<#define DATA_REGISTER_EMPTY (1<#define RX_COMPLETE (1<#define TX_COMPETE (1<int COM = 0; // if COM = 0 then use USART0 (ATmega128) if it = 1 then use USART1 (ATmega128)

#define notSS 0 // Порты
#define SCK 1
#define MOSI 2
#define MISO 3
#define TXD0 1
#define TXD1 3
#define DE1 7
#define DE0 6
#define RES0 5
#define RES1 4
#define RES2 3
#define RES3 2
#define RES4 1
#define RES5 0
#define PA16 7
#define PA17 6
#define PA18 5
#define RES6 4
#define RES7 1
#define RES8 0
#define RES9 7
#define RES10 6
#define RES11 5
#define RES12 4
#define RES13 3
#define WDTP 3
#define ALE 2
#define notRD 1
#define notWR 0

void InitPorts (void); // Инициализация портов микроконтроллера
void InitUSARTs (void);
void putchar (char c);
int getchar (void);

inline void InitPorts(void)
{
DDRA = 0x00; // Порт A - вход

DDRB = 0x00; // Порт B - вход
setbit(DDRB,notSS); // ~SS~ - выход
setbit(DDRB,SCK); // SCK - выход
setbit(DDRB,MOSI); // MOSI - выход
clrbit(DDRB,MISO); // MISO - вход
setbit(DDRB,DE1); // DE1 - выход
setbit(DDRB,DE0); // DE0 - выход
setbit(DDRB,RES0); // RES0 - выход
setbit(DDRB,RES1); // RES1 - выход

DDRC = 0xFF; // Порт C - выходы (PA8-PA15)

DDRD = 0x00; // Порт D - вход
setbit(DDRD,TXD1); // TXD1 - выход
setbit(DDRD,PA16); // PA16 - выход
setbit(DDRD,PA17); // PA17 - выход
setbit(DDRD,PA18); // PA18 - выход
setbit(DDRD,RES6); // RES6 - выход
setbit(DDRD,RES7); // RES7 - выход
setbit(DDRD,RES8); // RES8 - выход

DDRE = 0x00; // Порт E - вход
setbit(DDRE,TXD0); // TXD0 - выход
setbit(DDRE,RES9); // RES9 - выход
setbit(DDRE,RES10); // RES10 - выход
setbit(DDRE,RES11); // RES11 - выход
setbit(DDRE,RES12); // RES12 - выход
setbit(DDRE,RES13); // RES13 - выход

DDRF = 0x00; // Порт F - вход
setbit(DDRF,RES2); // RES2 - выход
setbit(DDRF,RES3); // RES3 - выход
setbit(DDRF,RES4); // RES4 - выход
setbit(DDRF,RES5); // RES5 - выход

DDRG = 0x00; // Порт G - вход
setbit(DDRG,WDTP); // WDTP - выход
setbit(DDRG,ALE); // ALE - выход
setbit(DDRG,notRD); // notRD - выход
setbit(DDRG,notWR); // notWR - выход
} // InitPorts()

inline void InitUSARTs (void)
{
UCSR0A=0x00; // USART0 initialization
UCSR0B=0x98; // Communication Parameters: 8 Data, 2 Stop, No Parity
UCSR0C=0x0E; // USART0 Receiver: On
UBRR0H=0x00; // USART0 Transmitter: On
UBRR0L=0x33; // USART0 Baud rate: 9600

UCSR1A=0x00; // USART1 initialization
UCSR1B=0x98; // Communication Parameters: 8 Data, 2 Stop, No Parity
UCSR1C=0x0E; // USART1 Receiver: On
UBRR1H=0x00; // USART1 Transmitter: On
UBRR1L=0x33; // USART1 Baud rate: 9600
}

#define _ALTERNATE_PUTCHAR_ // inform the compiler that an alternate version of the putchar function will be used
void putchar(char c) // now define the new putchar function
{
switch(COM)
{
case 0:
{
while ((UCSR0A & DATA_REGISTER_EMPTY)==0);
UDR0=c;
break;
}
case 1:
{
while ((UCSR1A & DATA_REGISTER_EMPTY)==0);
UDR1=c;
while ((UCSR1A & TX_COMPETE)==0);
}
};
}

#define _ALTERNATE_GETCHAR_ // inform the compiler that an alternate version of the getchar function will be used for USART1
int getchar(void) // now define the new getchar function
{
char status;
int data;
switch(COM)
{
case 0:
while (1)
{
while (((status=UCSR0A) & RX_COMPLETE)==0);
data=UDR0;
if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
return data;
};
case 1:
while (1)
{
while (((status=UCSR1A) & RX_COMPLETE)==0);
data=UDR1;
if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
return data;
};
};
return 0; // Исключительно для подавления warning'а - Warning[Pe940]: missing return statement at end of non-void function
}

Файл main.cpp


#include "main.hpp"
/* примеры команд:
W A FF - записать в порт А число 0xFF
W G 01 - записать в порт G число 0x01
R B XX - прочитать значение порта B (XX - два любых числа)
*/

int main()
{
char Command; // R(read) или W(write)
char CurrentPort; // A, B, C, D, E, F или G
uint PortValue; // 0h...FFh

InitUSARTs(); // Проинициализировали УАРТ'ы
InitPorts(); // Проинициализировали порты

COM = 1; // Работаем с USART1

while (1)
{
scanf("%c %c %x", &Command, &CurrentPort, &PortValue);

switch (Command)
{
case 'W':
switch (CurrentPort)
{
case 'A':
DDRA = 0xFF;
PORTA = (uchar) PortValue;
break;
case 'B':
DDRB = 0xFF;
PORTB = (uchar) PortValue;
break;
case 'C':
DDRC = 0xFF;
PORTC = (uchar) PortValue;
break;
case 'D':
DDRD = 0xFF;
PORTD = (uchar) PortValue;
break;
case 'E':
DDRE = 0xFF;
PORTE = (uchar) PortValue;
break;
case 'F':
DDRF = 0xFF;
PORTF = (uchar) PortValue;
break;
case 'G':
DDRG = 0xFF;
PORTG = (uchar) PortValue;
break;
default :
printf ("\r\n Неверно задан порт \r\n");
};
break;
case 'R':
switch (CurrentPort)
{
case 'A':
DDRA = 0x00;
PORTA = 0xFF;
PortValue = PINA;
break;
case 'B':
DDRB = 0x00;
PORTB = 0xFF;
PortValue = PINB;
break;
case 'C':
DDRC = 0x00;
PORTC = 0xFF;
PortValue = PINC;
break;
case 'D':
DDRD = 0x00;
PORTD = 0xFF;
PortValue = PIND;
break;
case 'E':
DDRE = 0x00;
PORTE = 0xFF;
PortValue = PINE;
break;
case 'F':
DDRF = 0x00;
PORTF = 0xFF;
PortValue = PINF;
break;
case 'G':
DDRG = 0x00;
PORTG = 0xFF;
PortValue = PING;
break;
default :
printf ("\r\n Неверно задан порт \r\n");
};
printf ("Порт %c = %x", CurrentPort, PortValue);
break;
default : printf ("\r\n Неверная команда (только W или R) \r\n");
}
}
} // main()


Составить ответ  |||  Конференция  |||  Архив

Ответы



Перейти к списку ответов  |||  Конференция  |||  Архив  |||  Главная страница  |||  Содержание  |||  Без кадра

E-mail: info@telesys.ru