I'm working on a PWM app using T0 on the mega48 and when I finally started running on the STK500 I had problems connecting to the board after programming the chip the first time. The first chip I used had been programed before with some other app. The board will fire up and the m48 will run the app that I put on it, but after making some mods to the code I went to reburn and Studio 4.12 b490 wouldn't connect to the board. What the heck I thought... so I checked all the typical things that go wrong for me, no joy. So I pull the m48 and Studio connects to the STK500 no problem. Put the chip back in, no good. try a new m48 and happiness, so I burn the new hex file with success and it performs as expected. I try to connect to the board from Studio, no good. I'm using COM1 RS232 port (no USB converter) Brayterm is running on COM2, so I rescan in Brayterm and it says COM1 is available.
What throws me in my attempts to trouble shoot is what about my program disables Studio to even connect to the STK500. I did check that no fuses were programed other than the default, I'm running the internal 8MHz.
Has anyone else ever dealt with this or similar problem? I would appreciate any suggestions / help.
Thank you.
/*! \file ******************************************************************* * * File : DC_MotorControler.c * Compiler : GCC (WinAVR) * Revision : 1.0 * Date : 06/21/09 * Updated by : Ben Nemec * * Supported devices : All devices with a PWM can be used. * The example is written for ATmega48 * * Description : controll speed of a brush type DC motor using PWM and * an H-bridge circuit, control from UART, SPI or TWI port * ****************************************************************************/ #include#include #include #include #include "DC_MotorController.h" #include "UART.h" #include "ADC.h" //constants #define FALSE 0 #define TRUE (!FALSE) // Private Macro definitions #define sbi(port,bit) (port |= (1<<bit)) //set bit in port #define cbi(port,bit) (port &= ~(1<<bit)) //clear bit in port #define PWM PD6 #define HBp 4 #define CNTLA PORTC #define FAULT PC2 //fault signal input from A3940 #define RSTn 0 //pwer down & clear latched faults #define PHASE 1 //motor direction control // #define ENBL 2 //high allows direct control of driver outputs via phase #define SR PC3 //sychornous rectification #define MODE 4 //current decay mode // private function prototypes: void init_counter(void); void InitMotorControler (void); /* Main */ int main( void ) { //dissable all interrupts cli(); //crank clock up to 8MHz see datasheet doc2545 p35 //CLKPR = 0x80; //must write CLKPCE = 1 at same time as rest to 0 //CLKPR = 0x00; //program to divide by 1 // not calibrating osc. //configure heartbeat pin: sbi (DDRD, HBp); sbi (PORTD, HBp); volatile uint16_t hrtBtCounter = 0; volatile uint16_t i; volatile uint8_t data; for (i = 0; i< 10; i++) { for (hrtBtCounter = 0; hrtBtCounter < 50; hrtBtCounter ++); cbi (PORTD, HBp); for (hrtBtCounter = 0; hrtBtCounter < 50; hrtBtCounter ++); sbi (PORTD, HBp); } // cbi (PORTD, HBp); init_counter(); init_adc(); USART_Init(25); SendNTString("UART Initialized\n\r"); InitMotorControler(); hrtBtCounter = 0; for (;;) //ever { data = USART_Receive(); if (data >= 0x30 && data <= 0x39) OCR0A = (data - 0x30) * 28; } return 0; } //************************* init_counter ********************************** /* Phase Correct PWM Mode (WGM02:0 = 1), TOP = 0xFF non-inverted PWM; COM0x1:0 bits = two. 0 0 1 clkI/O/(No prescaling) */ void init_counter(void) { cbi (PRR, PRTIM0); //setup counter frequency //0x24 (0x44) TCCR0A [COM0A1 COM0A0 COM0B1 COM0B0 "“ "“ WGM01 WGM00] TCCR0A = 0x81; // [ 1 0 0 0 0 0 0 1 //0x25 (0x45) TCCR0B [FOC0A FOC0B "“ "“ WGM02 CS02 CS01 CS00] TCCR0B = 0x01; // [ 0 0 0 0 0 0 0 1 ] } //************************* InitMotorControler ***************************/ void InitMotorControler (void) { init_counter(); // Initiate and starts Counter 0 for PWM sbi(DDRD, PWM); // set pin as output for PWM sbi(DDRC, RSTn); // set port pin directions for control sbi(DDRC, PHASE); // set port pin directions for control // sbi(DDRC, ENBL); // set port pin directions for control // sbi(DDRC, SR); // set port pin directions for control // sbi(DDRC, MODE); // set port pin directions for control //powerup the Allegro A3940 according to the app note. // cbi(CNTLA, ENBL); sbi(CNTLA, RSTn); _delay_us(10); //allow time for vregs in A3940 to come up. // sbi(CNTLA, SR); // sbi(CNTLA, MODE); }