Thursday, 23 August 2012

Testing IO of Micro

This is a simple test to confirm all the pins are functioning as intended on your micro.  This is for a 32 channel device and using a 32 channel logic analyser (LogicPort).


#include <avr/io.h>
#include <stdint.h>
#include <avr/delay.h>
#include "global.h"

int main(void)
{
DDRA = DDRB = DDRC = DDRD = 0xFF;
    while(1)
    {
        int i;
for (i=0; i<8; i++){
PORTA = PORTB = PORTC = PORTD |= (1<<i);
_delay_ms(50);
PORTA = PORTB = PORTC = PORTD = 0;
}
    }
}


AVR PORT control via RS232 and C# GUI

I was surprised how simple this was.  I have been studying Visual C++ for sometime and this project would be very difficult to achieve using that.  With C#,however, it is very simple and this interface can be knocked up in a couple of hours.  (This is actually the first time for me to use it!).  If you have previous experience with Visual Basic it is useful with the controls etc.

The following is a capture of the logic analyzer and the GUI.


The logic analyser is a34 channel LogicPort which I find really useful.  You can see that it has an interpreter attached to both the transmit and receive to show the ASCII character in the frame.

The AVR code is set up using interrupt control of the USART to transmit and receive.  It receives one byte from the serial port and assigns it to PORTA, which is connected to the LEDs on the STK500 board.  It then reassigns the same value to the UDR register which will fire the transmit interrupt to echo back to the program on the PC.  The STK500 is not shown but as we select each check box in the video below the appropriate LED goes on/off.  The video starts by showing how you select the serial port and the exception handling of the C# program.



The following is the C code for the AVR (mega32 in this case).  For simplicity I have not shown the seperate files but the Usart init is in its own header file..



Friday, 4 May 2012

Some C code for PWM

Simple PWM Project

/*
 * Simple_PWM.c
 *
 * Created: 10/04/2012 8:13:17 PM
 * 
 * PINC pins 0 and 1 for duty select, via dip switch
 */

#include <avr/io.h>

#include <stdint.h>
#include "timer.h"
#include "global.h"

#define PWM_select (PINC & 3)


int main(void)
{
    const uint8_t resolution = 8;
    uint8_t old_sw;        // stores previous switch status
   
    // set up ports
    DDRD = 0x20;        // set OC1A (PWM) out
    PORTC = 0x03;        // set pull up resistors for pwm dsw select
    // timer1 settings
    timer1Init();        // init timer1 vis timer lib
    timer1PWMInit(resolution);  // init PWM
    timer1PWMAOn();        // turn on PWM
   
    sei();
   
    while(1)
    {
        if(PWM_select != old_sw){
            old_sw = PWM_select;
            switch (PWM_select)
            {
            case 0: timer1PWMASet(dutyCyclePercent(5,resolution));    // 5% duty
                break;
            case 1:    timer1PWMASet(dutyCyclePercent(30,resolution));    // 30% duty
                break;
            case 2: timer1PWMASet(dutyCyclePercent(60,resolution));    // etc
                break;
            case 3:    timer1PWMASet(dutyCyclePercent(95,resolution));
                break;
            default:
            break;
            }
        }
    }
}