Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Help Verifying this C code

Status
Not open for further replies.

borge

Junior Member level 1
Joined
Jan 4, 2013
Messages
16
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Norway
Activity points
1,375
Hi
I am new to microcontrollers, and programming. But have basic skills in electronics.

I have buildt a Digital multiple powersupply with the Att2313. https://www.electronics-lab.com/projects/power/020/index.html
I programmed the att2313 with my programmer (willem PCB50B ) and it verified it as ok. I used the ICSP connection.
But it doesnt work, i checked the circuit, and all is ok.
I checked the C code in DEV-C++ and it found errors ??

I Hope somone can verify the code for me, so i know if i need to program again, i might have wrong settings when programming

The code.

Code:
/*
 MCU Power Supply

 3/7/2010 BEER-WARE LICENSE
 Garrett Fogerlie wrote this file. As long as you retain this notice you
 can do whatever you want with this stuff. If we meet some day, and you think
 this stuff is worth it, you can buy me a beer in return

 AtTiny2313 @ internal 4Mhz
*/

#include <avr/io.h>
#define F_CPU 4000000UL
#include <util/delay.h>

uint8_t buttons;
uint8_t outPin = 1;

int main(void)
{

	while(1)
	{
		buttons = (PIND & 0x0C);// This will store the value of PD2 and PD3 (PD2=0x04, and PD3=0x08, so together it's 0x0C)
		_delay_ms(35);// A small debounce delay
		if(buttons == 0x04)// If PD2 (-) was pressed
		{
			if(outPin == 0x01)// If its at its lowest value and DOWN is pressed,
			{
				outPin = 0x10;//  roll over to the highest value (0x10)
			}
			else // If its not at its lowest value
			{
				outPin >>= 1;// lower it by a power of 2 (bit shift it to the right by 1)
			}

			PORTB = outPin;// Set the output port to our outPin value (this will make it output high on the pin that corresponds to outPin's value)
		}
		if(buttons == 0x08)// If PD3 (+) was pressed
		{
			if(outPin == 0x10)// If its at its highest value and UP is pressed,
			{
				outPin = 0x01;//  roll over to the lowest value (0x01)
			}
			else // If its not at its highest value
			{
				outPin <<= 1;// lower it by a power of 2 (bit shift it to the right by 1)
			}

			PORTB = outPin;// Set the output port to our outPin value (this will make it output high on the pin that corresponds to outPin's value)
		}
	}
	return 0;
}

Thanks

Børge

- - - Updated - - -

Thanks alexan_e, i should have read the posting rules first.

Are you able to se if this code is ok.

Børge
 
Last edited by a moderator:

Good news.
I finally got the tiny2313 programmed, i had to use the AVR Studio to compile the source file, and get a HEX file.
I have the Willem PCB 50 programmer, and i used a extrernal isp adapter.

Just one minor problem left, the leds that indicate selected voltage level lights up weak.
 

Are the leds connected like this

mcu_power_supply_schematic_th.jpg


You can either use leds that require less current or reduce the base resistor to say 150
 

Hi alexan_e

Yes, thats the chema, i will change them tomorrow.
When i the circuit to worked ok i had to have a brake from the intense research i did to make it work.
But i am satisfied that i have made my first circuit with a Micro controller.

And i have some more projects i want to try, so i look forward to it.

Børge
 

You can also try will a red led, they have a lower Vf and may produce more light.

If you want to measure the led current measure the voltage across the resistor and divide it with the resistor value, I think about 10mA would be fine
 

I changed them to 180, but didnt help a lot. tried 150, but not enough.
I guess its 5 volt on the 12-16 pins
I use red leds, but they might draw to much current.
 

You can't calculate the current exactly based on a 5v output because the output of AVR depends on the current you source, see this

avr.jpg

It is from mega8 but it gives you an idea.

You can accurately measure the led current by measuring the voltage across the resistor as I described in the previous post.
Also check the datasheet of your device for max current.
 

It can't be seen from the code, if the port is initialized as output at all. Presuming this is the case, LEDs of very poor efficiency or unsuitable switch transistor (e.g. darlington) are the only explanations that come to my mind.

To be sure, you should however measure the individual voltage drops of transistor, LED and series resistor of an activated output. Which exact transistor type are you using?
 
  • Like
Reactions: tpetar

    tpetar

    Points: 2
    Helpful Answer Positive Rating
It can't be seen from the code, if the port is initialized as output at all.

True, I didn't notice that, I wrongly assumed the pins were set as outputs.
 
  • Like
Reactions: borge

    borge

    Points: 2
    Helpful Answer Positive Rating
It can't be seen from the code, if the port is initialized as output at all. Presuming this is the case, LEDs of very poor efficiency or unsuitable switch transistor (e.g. darlington) are the only explanations that come to my mind.

To be sure, you should however measure the individual voltage drops of transistor, LED and series resistor of an activated output. Which exact transistor type are you using?

Hi, i use the 2N2222. I tried several other LEDs, but didnt help.
I tried different power supplys: from 12v/0.5Amp to 18v 1,5 Amp.
I have a R-typeTransformer that has 32,5V/AC - 44V-DC rectified, but thats to high voltage for this circuit, max input is 33V/DC.
Have to find out how to regulate this down.
 

2N2222 is good. The power supply point isn't related to the original discussed problem, I think. I presume so far, that the microcontroller is supplied with 5V.
 
  • Like
Reactions: borge

    borge

    Points: 2
    Helpful Answer Positive Rating
What about the PORT setting , did you set it as output?
If not use DDRB |= 0x1F; (in the start of main) and check the result.
 
  • Like
Reactions: borge

    borge

    Points: 2
    Helpful Answer Positive Rating
What about the PORT setting , did you set it as output?
If not use DDRB |= 0x1F; (in the start of main) and check the result.

The picture shows measured values loaded, and unloaded.
The MCU has 5volt in.
mcu_power_supply_schematic.png


This is the code i programmed the att2313 with.
At the end is says PORTB = outPin;//

Code:
/*
 * MCU_powr_supply.c
 *
 * Created: 05.01.2013 19:53:38
 *  Author: Borge
 */ 


#include <avr/io.h>
#define F_CPU 4000000UL
#include <util/delay.h>

uint8_t buttons;
uint8_t outPin=1;

int main(void)
{
    while(1)
    {
      buttons = (PIND & 0x0C);// This will store the value of PD2 and PD3 (PD2=0x04, and PD3=0x08, so together it's 0x0C)
      _delay_ms(35);// A small debounce delay
      if(buttons == 0x04)// If PD2 (-) was pressed
      {
	      if(outPin == 0x01)// If its at its lowest value and DOWN is pressed,
	      {
		      outPin = 0x10;//  roll over to the highest value (0x10)
	      }
	      else // If its not at its lowest value
	      {
		      outPin >>= 1;// lower it by a power of 2 (bit shift it to the right by 1)
	      }

	      PORTB = outPin;// Set the output port to our outPin value (this will make it output high on the pin that corresponds to outPin's value)
      }
      if(buttons == 0x08)// If PD3 (+) was pressed
      {
	      if(outPin == 0x10)// If its at its highest value and UP is pressed,
	      {
		      outPin = 0x01;//  roll over to the lowest value (0x01)
	      }
	      else // If its not at its highest value
	      {
		      outPin <<= 1;// lower it by a power of 2 (bit shift it to the right by 1)
	      }

	      PORTB = outPin;// Set the output port to our outPin value (this will make it output high on the pin that corresponds to outPin's value)
      }
  //TODO:: Please write your application code 
    }
	return 0;
}
 
Last edited:

I think you need to read

If you don't set the direction register to output mode then the port register just enables the week pull up resistors.
 
  • Like
Reactions: borge

    borge

    Points: 2
    Helpful Answer Positive Rating
Hi alexan_e

Finally got it to work, studying made the trick.
The Ports was not set as output.

Børge

What about the PORT setting , did you set it as output?
If not use DDRB |= 0x1F; (in the start of main) and check the result.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top