Help Needed Please..uSupply REV.C Code

Go To Last Post
39 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Greetings!

 

I recently tried to rework uSupply rev.c with my own code and i took the schematics and created new code for my Atmega16A so that it operates my power supply. But I dunno why my while(1) loop doesn't start. please check my code and help me please because something wrong with my code.

Notes:

 

1- Micro-controller: ATMEGA16A.

2- Clock speed: 16MHz.

3- LFuse: 0xFF

4- HFuse: 0xD9

5- EEVBLOG uSupply Rev.c (for people who doesn't know and will start saying things i don't like).

6- Rework done: (for people who like to talk much).

A- Changed how the unit main voltage power rail to an offline transformer and a dc plug-in.

B- Changed the LCD to 16x2.

C- Eliminated the Ethernet, The Rotary and the DAC.

 

7- Inspection done to Hardware and the result is WORKING!. 

8- I don't have a debugging tool.  

9- Please be subjective and don't just insult or attack with words.

Thanks!!!

PSU.c:

#include <\Power Supply\Program\PSU\PSU\MrLCD.h>
#include <\Power Supply\Program\PSU\PSU\PSU.h>

	volatile int global = 0; //Variable for buttons selection 0=idle, 1=set button active, 2=start button active and 3=stop button active
	volatile int setstatus = 0; //Variable for "Set" button selection (Multi-function Button)
	volatile int devicestatus = 0; //Variable for working condition of the device 0=idle, 1=on and  2=off
	volatile double vsetp = 0.00; //Variable for Voltage Pot
	volatile double isetp = 0.00; //Variable for Current Pot
	volatile double vref = 5.81; //AVCC of Atmega16
	volatile double vsetgain = 4; //Voltage gain of voltage selection Op-Amp
	volatile double maxcurrent = 3.50; //Max current allowed
	volatile double regref = 1.25; //Voltage ref of voltage regulator
	volatile double mainvolt = 00.0; //Main voltage 
	volatile double voltout = 00.00; //Voltage output
	volatile double Currentout = 00.00; //Current Output
	volatile double mainvoltdividerratio = 5.125; //Resistor voltage divider ratio for main voltage
	volatile double voltoutdividerratio = 4.375; //Resistor Voltage divider ratio for output voltage
	volatile uint16_t adc_result; //Variable for ADC read

int main(void)
{
	sei(); // Enable Interrupt
	InitializeMrLCD();	//initialization LCD
	InitADC(); //initialization of ADC
	Enable_Interface(); // Setting bits for buttons and leds

    while(1)
    {
		if(BON_PORT & (1<<BON)) //Check if On/Off button is activated
		{
			if(devicestatus == 0) //check if device condition is idle
			{
				devicestatus = 1; //set device condition to ON
			} else if(devicestatus == 1) //check if device condition is already ON
			 {
				devicestatus = 2; //set device condition to OFF
			 } else {
				 devicestatus = 0; //set device condition to idle
			 }
		}
		switch(devicestatus) //switch statements for device condition
			{
				case 0: //Idle
					clearBit(RELAY_PORT, RELAY); //Close Output relay
					break;
				case 2: //OFF
					Enable_OffLED(); 
					Disable_Interrupts();
					Disable_PWM();
					clearBit(RELAY_PORT, RELAY);
					clearBit(OVERLED_PORT, OVERLED);
					Send_A_StringToMrLCDWithLocation(5, 1, "Tronix");
					Send_A_StringToMrLCDWithLocation(2, 2, "Power Supply");
					Send_A_Command(0x18);
					Send_A_Command(0x01);
					Send_A_Command(0x08);
					break;
				case 1: //ON
					Enable_OnLED();
					Enable_Interrupts();
					Send_A_Command(0x0C);
					Send_A_StringToMrLCDWithLocation(5, 1, "Tronix");
					Send_A_StringToMrLCDWithLocation(2, 2, "Power Supply");
					Send_A_Command(0x1C);
					adc_result = ReadADC(0xC0);
					mainvolt = ((adc_result * vref)/1024)*mainvoltdividerratio;
					Send_A_StringToMrLCDWithLocation(2, 1, "Main Voltage");
					Send_An_DoubleToMrLCD(6, 2, mainvolt, 4);
					Send_A_Command(0x01);
					Send_A_StringToMrLCDWithLocation(1 ,1, "VSet:00.00 00.00");
					Send_A_StringToMrLCDWithLocation(1 ,2, "ISet:00.00 00.00");
					break;
					switch(global)
					{
						case 1: //Set button is active
							clearBit(RELAY_PORT, RELAY);
							clearBit(OVERLED_PORT, OVERLED);
							Disable_PWM();
							switch(setstatus)
							{
								case 1: //Voltage Set
									while(setstatus == 1)
									{
										adc_result = ReadADC(0xC3);
										vsetp = ((adc_result * vref)/1024)*vsetgain;
										GotoMrLCDsLocation(6, 1);
										Send_A_Command(0x0E);
										Send_An_DoubleToMrLCD(6, 1, vsetp, 5);
									}
							break;
								case 2: //Current Set
									while(setstatus == 2)
									{
										adc_result = ReadADC(0xC4);
										isetp = ((adc_result * vref)/1024);
										if(isetp > maxcurrent)
										{
											isetp = maxcurrent;
										}
										GotoMrLCDsLocation(6, 2);
										Send_A_Command(0x0E);
										Send_An_DoubleToMrLCD(6, 2, isetp, 5);
									}
									break;
								case 0: //Idle condition
										Send_A_Command(0x08);
										Send_A_Command(0x0C);
							}
							break;
						case 2: // Start button is active
							OCR1A = ICR1 - (((vsetp - regref)/vsetgain)/5)*ICR1;
							OCR1B = ICR1 - (isetp/5)*ICR1;
							Enable_PWM();
							Send_A_Command(0x0C);
							setBit(RELAY_PORT, RELAY);
							while (setstatus == 2)
							{
								if(!(CLIM_PORT & (1<<CLIM))) //Check over current pin
								{
									setBit(OVERLED_PORT, OVERLED); //Light over current led
									adc_result = ReadADC(0xC1);
									voltout = ((adc_result * vref)/1024)*voltoutdividerratio;
									Send_An_DoubleToMrLCD(12, 1, voltout, 5);
									adc_result = ReadADC(0xC2);
									Currentout = ((adc_result * vref)/1024);
									Send_An_DoubleToMrLCD(12, 2, Currentout, 5);
								} else {
									clearBit(OVERLED_PORT, OVERLED);
									adc_result = ReadADC(0xC1);
									voltout = ((adc_result * vref)/1024)*voltoutdividerratio;
									Send_An_DoubleToMrLCD(12, 1, voltout, 5);
									adc_result = ReadADC(0xC2);
									Currentout = ((adc_result * vref)/1024);
									Send_An_DoubleToMrLCD(12, 2, Currentout, 5);
								}
							}
							break;
						case 0: //Stop button is active
							clearBit(RELAY_PORT, RELAY);
							Disable_PWM();
							clearBit(OVERLED_PORT, OVERLED);
							break;
					}
			}
	}

}
ISR(INT0_vect) // INT for Set button
{
	global = 1;
	if(setstatus == 0)
	{
		setstatus = 1;
	} else if(setstatus == 1)
		{
			setstatus = 2;
		} else {
				setstatus = 0;
		}
}

ISR(INT1_vect) //INT for start button
{
	global = 2;
}

ISR(INT2_vect) //INT for stop button
{
	global = 0;
}

 

PSU.h:

#include <avr/interrupt.h>

#define		BSTOP							PB2 //Stop button
#define 	BSTOP_DDR						DDRB
#define 	BSTOP_PORT						PORTB
#define 	BON								PB4 //On/Off Button
#define 	BON_DDR							DDRB
#define 	BON_PORT						PORTB
#define 	OVERLED							PD7 //Over Current Led
#define 	OVERLED_DDR						DDRD
#define 	OVERLED_PORT					PORTD
#define		CLIM							PD6 //Over Current Op-Amp Output
#define		CLIM_DDR						DDRD
#define		CLIM_PORT						PORTD
#define		BSTART							PD3 //Start Button
#define		BSTART_DDR						DDRD
#define		BSTART_PORT						PORTD
#define		BSET							PD2 /Set Button
#define		BSET_DDR						DDRD
#define		BSET_PORT						PORTD
#define		RELAY							PD1 //Relay Transistor
#define		RELAY_DDR						DDRD
#define		RELAY_PORT						PORTD
#define		ONOFFLED						PD0 //On Led
#define		ONOFFLED_DDR					DDRD
#define		ONOFFLED_PORT					PORTD
#define		VSET							PD5 //PWM Pin for Voltage Op-Amp 
#define		VSET_DDR						DDRD
#define		VSET_PORT						PORTD
#define		ISET							PD4 //PWM pin for Current Op-Amp
#define		ISET_DDR						DDRD
#define		ISET_PORT						PORTD
#define		setBit(sfr, bit)				(_SFR_BYTE(sfr) |= (1 << bit))
#define		clearBit(sfr, bit)				(_SFR_BYTE(sfr) &= ~(1 << bit))
#define		toggleBit(sfr, bit)				(_SFR_BYTE(sfr) ^= (1 << bit))
#define		PWMSpeed						8000 //PWM Speed

void Enable_Interface(void); //Set Button directions High or Low
void Enable_Interrupts(void); // Set INT
void Enable_OffLED(void); //Enable On LED
void Enable_OnLED(void);
void Disable_Interrupts(void);
void InitADC(void);
uint16_t ReadADC(uint8_t ch);
void Enable_PWM(void);
void Disable_PWM(void);

void Enable_Interface()
{
	clearBit(BON_DDR, BON);
	setBit(ONOFFLED_DDR, ONOFFLED);
	clearBit(BSTOP_DDR, BSTOP); //Enable Stop Button
	clearBit(BSTOP_PORT, BSTOP);
	setBit(OVERLED_DDR, OVERLED);	//Enable Over-Current Led
	clearBit(OVERLED_PORT, OVERLED);
	clearBit(CLIM_DDR, CLIM);	//Enable Over--Current Sense
	setBit(CLIM_PORT, CLIM);
	clearBit(BSTART_DDR, BSTART);	//Enable Start Button
	clearBit(BSTART_PORT, BSTART);
	clearBit(BSET_DDR, BSET);	//Enable Set Button
	clearBit(BSET_PORT, BSET);
	setBit(RELAY_DDR, RELAY);	//Enable Relay Control
	setBit(VSET_DDR, VSET);  //Enable VSET PWM
	setBit(ISET_DDR, ISET);  //Enable ISET PWM
}

void Enable_Interrupts()
{
	MCUCR = (1 << ISC11) | (1 << ISC10) | (1 << ISC01) | (1 << ISC00);	//Set Interrupts to work on Raising Edge for INT0 and INT1
	MCUCSR = (1 << ISC2);	//Set Interrupts to work on Raising Edge for INT2
	GICR = (1 << INT1) | (1 <<INT0) | (1 << INT2);	//Enable Interrupts
}

void Enable_OffLED()
{
	clearBit(ONOFFLED_PORT, ONOFFLED);
}

void Enable_OnLED()
{
	setBit(ONOFFLED_PORT, ONOFFLED);
}

void Disable_Interrupts()
{
	GICR &= ~(1 << INT1) | ~(1 <<INT0) | ~(1 << INT2);	//Disable Interrupts
}

void InitADC()
{
	ADMUX |= (1<<REFS0);
	ADCSRA |= (1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);
}

uint16_t ReadADC(uint8_t ch)
{
	ch = ch & 0b01011111;
	ADMUX = ch;
	ADCSRA |= (1<<ADSC);
	while(!(ADCSRA & (1<<ADIF)));
	ADCSRA |= (1<<ADIF);
	return(ADC);
}

void Enable_PWM()
{
	TCCR1A |= (1<<WGM11) | (1<<COM1A1) | (1<<COM1A0) | (1<<COM1B1) | (1<<COM1B0);
	TCCR1B |= (1<<WGM13) | (1<<WGM12) | (1<<CS10);
	ICR1 = (F_CPU / PWMSpeed);
}

void Disable_PWM()
{
	TCCR1B &= ~(1<<CS10);
}

 

MrLCD.h:

#define		F_CPU							16000000L
#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>

#define MrLCDsCrib PORTC
#define DataDir_MrLCDsCrib DDRC
#define MrLCDsControl PORTB
#define DataDir_MrLCDsControl DDRB
#define LightSwitch PB0
#define ReadWrite PB1
#define BiPolarMood PB3

char firstColumnPositionsForMrLCD[4] = {0, 64, 20, 84};

void Check_IF_MrLCD_isBusy(void);
void Peek_A_Boo(void);
void Send_A_Command(unsigned char command);
void Send_A_Character(unsigned char character);
void Send_A_String(char *StringOfCharacters);
void GotoMrLCDsLocation(uint8_t x, uint8_t y);
void Send_A_StringToMrLCDWithLocation(uint8_t x, uint8_t y, char *StringOfCharacters);
void Send_An_DoubleToMrLCD(uint8_t x, uint8_t y, double DoubleToDisplay, char NumberOfDigits);
void InitializeMrLCD(void);

void Check_IF_MrLCD_isBusy()
{
	DataDir_MrLCDsCrib = 0;
	MrLCDsControl |= 1<<ReadWrite;
	MrLCDsControl &= ~1<<BiPolarMood;

		while (MrLCDsCrib >= 0x80)
		{
			Peek_A_Boo();
		}
	DataDir_MrLCDsCrib = 0xFF; //0xFF means 0b11111111
}

void Peek_A_Boo()
{
	MrLCDsControl |= 1<<LightSwitch;
	asm volatile ("nop");
	asm volatile ("nop");
	MrLCDsControl &= ~1<<LightSwitch;
}

void Send_A_Command(unsigned char command)
{
	Check_IF_MrLCD_isBusy();
	MrLCDsCrib = command;
	MrLCDsControl &= ~ ((1<<ReadWrite)|(1<<BiPolarMood));
	Peek_A_Boo();
	MrLCDsCrib = 0;
}

void Send_A_Character(unsigned char character)
{
	Check_IF_MrLCD_isBusy();
	MrLCDsCrib = character;
	MrLCDsControl &= ~ (1<<ReadWrite);
	MrLCDsControl |= 1<<BiPolarMood;
	Peek_A_Boo();
	MrLCDsCrib = 0;
}

void Send_A_String(char *StringOfCharacters)
{
	while(*StringOfCharacters > 0)
	{
		Send_A_Character(*StringOfCharacters++);
	}
}

void GotoMrLCDsLocation(uint8_t x, uint8_t y)
{
	Send_A_Command(0x80 + firstColumnPositionsForMrLCD[y-1] + (x-1));
}

void Send_A_StringToMrLCDWithLocation(uint8_t x, uint8_t y, char *StringOfCharacters)
{
	GotoMrLCDsLocation(x, y);
	Send_A_String(StringOfCharacters);
}

void Send_An_DoubleToMrLCD(uint8_t x, uint8_t y, double DoubleToDisplay, char NumberOfDigits)
{
	char StringToDisplay[NumberOfDigits];
	dtostrf(DoubleToDisplay, NumberOfDigits, 2, StringToDisplay);
	Send_A_StringToMrLCDWithLocation(x, y, StringToDisplay);
	Send_A_String(" ");
}

void InitializeMrLCD()
{
	DataDir_MrLCDsControl |= 1<<LightSwitch | 1<<ReadWrite | 1<<BiPolarMood;
	_delay_ms(15);

	Send_A_Command(0x01); //Clear Screen 0x01 = 00000001
	_delay_ms(2);
	Send_A_Command(0x38);
	_delay_us(50);
	Send_A_Command(0b00001110);
	_delay_us(50);
}

Best Regards

Ahmed Hamdy 

This topic has a solution.

Humility, O humans. We are just a worm's meal in the grave!

Last Edited: Wed. Nov 6, 2019 - 01:21 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Please give your thread a more descriptive title - "help needed please" tells us nothing at all, and the same title could be applied to nearly every thread in the whole community!

 

AhmedHamdy wrote:
I recently tried to rework uSupply rev.c

What is a "uSupply rev.c" ?

 

What "rework", exactly, have you done ?

 

If it is hardware "rework", are you sure that the hardware is now working ?

 

AhmedHamdy wrote:
please check my code

So what checking have you done already?

 

AhmedHamdy wrote:
Micro-controller: ATMEGA16A

The ATmega16A has on-chip debug - have you used it to see what's happening?

 

Have you tried some basic code to blink LEDs & send messages through the UART?

 

Then use these facilities to help to see where your code is getting to 

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:

Edited main thread with information required and please be more subjective next time

 

awneil wrote:

Please give your thread a more descriptive title - "help needed please" tells us nothing at all, and the same title could be applied to nearly every thread in the whole community!

 

AhmedHamdy wrote:
I recently tried to rework uSupply rev.c

What is a "uSupply rev.c" ?

 

What "rework", exactly, have you done ?

 

If it is hardware "rework", are you sure that the hardware is now working ?

 

AhmedHamdy wrote:
please check my code

So what checking have you done already?

 

AhmedHamdy wrote:
Micro-controller: ATMEGA16A

The ATmega16A has on-chip debug - have you used it to see what's happening?

 

Have you tried some basic code to blink LEDs & send messages through the UART?

 

Then use these facilities to help to see where your code is getting to 

 

 

 

Edited main thread with information required and please be more subjective next time

Humility, O humans. We are just a worm's meal in the grave!

Last Edited: Tue. Nov 5, 2019 - 03:03 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


It's the title that needs editing - not the post!

 

Other people seem to manage to give their threads titles which actually indicate what the thread is about:

 

 

Editing a post after it has received replies is unhelpful & confusing.

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:

It's the title that needs editing - not the post!

 

Other people seem to manage to give their threads titles which actually indicate what the thread is about:

 

 

Editing a post after it has received replies is unhelpful & confusing.

 

 

 

Done Sir

Humility, O humans. We are just a worm's meal in the grave!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

AhmedHamdy wrote:
Please be subjective and don't just insult or attack with words

I am being entirely objective and there is no attack.

 

remember that we know nothing about you, we cannot see what you are doing, we cannot see what you have, and we do not know what you have done.

 

therefore you need to describe all these things clearly and completely in your posts.

 

Where things are not clear, we have to ask you to clarify them.

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

OK Sir. Done what you requested Thank you for your observation

Humility, O humans. We are just a worm's meal in the grave!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

AhmedHamdy wrote:
Done Sir

Thank you.

 

AhmedHamdy wrote:
5- EEVBLOG uSupply Rev.c (

Thank you.

 

So is the code that you posted entirely your own, or based on Dave's code?

 

It is always best to start with known-good code, and make just small changes at a time - and test each step ...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:

AhmedHamdy wrote:
Done Sir

Thank you.

 

AhmedHamdy wrote:
5- EEVBLOG uSupply Rev.c (

Thank you.

 

So is the code that you posted entirely your own, or based on Dave's code?

 

It is always best to start with known-good code, and make just small changes at a time - and test each step ...

My Code (Entirely Mine). Dave's code isn't opened for the public sir

Humility, O humans. We are just a worm's meal in the grave!

Last Edited: Tue. Nov 5, 2019 - 03:14 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

AhmedHamdy wrote:
8- I don't have a debugging tool

If you are serious about embedded development, I strongly suggest that you do get a debugger!

 

It is an invaluable tool, precisely because it lets you look inside the processor to see what's happening, step through your code to see what it's doing, etc, etc.

 

If you don't have a debugger, you are going to have to do it the old-fashioned way by instrumenting you code so that it tells you what it is doing.

 

Common techniques, as I said, include:

 

  • Blinking LEDs on key events;
  • Toggling pins so that you can observe them on a 'scope;
  • Using the UART to send debug messages.

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am using the old  fashion Because debugger tool is in Dollar and that is so expensive for me. (POOR MAN WAY)

Humility, O humans. We are just a worm's meal in the grave!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

AhmedHamdy wrote:
My Code (Entirely Mine).

OK.

 

So, before loading the large project, did you start with a simpler project doing basic things like flashing an LED and sending a message out of the UART?

 

This is always the way to start!

 

Otherwise, if you just write one huge lump of code and it doesn't work, you have no idea where to start!

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:

 

So, before loading the large project, did you start with a simpler project doing basic things like flashing an LED and sending a message out of the UART?

 

This is always the way to start!

 

Otherwise, if you just write one huge lump of code and it doesn't work, you have no idea where to start!

 

That is not my first project sir. Would you help me please with the mentioned code (Something is missing which i don't see in the logic). I NEED A SECOND EYE. OMG

Humility, O humans. We are just a worm's meal in the grave!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well the very first thing that strikes me about this code is:

int main(void)
{
	int global = 0;

then:

ISR(INT0_vect)
{
	global = 1;
	if(setstatus == 0)
	{
		setstatus = 1;
	} else if(setstatus == 1)
		{
			setstatus = 2;
		} else {
				setstatus = 0;
		}
}

ISR(INT1_vect)
{
	global = 2;
}

ISR(INT2_vect)
{
	global = 0;
}

Even if "global" had really been a global this was not going to work as it needs to be volatile. But worse than that it is LOCAL to the main() function. In fact I'm astonished the code compiles as the ISRs seem to be accessing a variable that does not exist!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You are right!

But i had all the variables before the in main(void). and i tried it and it doesn't work. 

And it indeed compile the code like that.

I updated the main code on the main thread 

but it still doesn't work with your suggestion

Humility, O humans. We are just a worm's meal in the grave!

Last Edited: Tue. Nov 5, 2019 - 03:39 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

AhmedHamdy wrote:
But i had all the variables before the in main(void). and i tried it and it doesn't work. 
How is randomly changing the scope of variables suddenly supposed to make code run?

 

Surely the code either works according to your design notes or it doesn't? If it doesn't then see how it varies from the design or what it is about the design that is fundamentally wrong.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:

AhmedHamdy wrote:
But i had all the variables before the in main(void). and i tried it and it doesn't work. 
How is randomly changing the scope of variables suddenly supposed to make code run?

 

Surely the code either works according to your design notes or it doesn't? If it doesn't then see how it varies from the design or what it is about the design that is fundamentally wrong.

 

The scope of the variables is now like the main code above but it still doesn't work and i can post the schematic to check it if you need.

Humility, O humans. We are just a worm's meal in the grave!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The Schematics

 

Attachment(s): 

Humility, O humans. We are just a worm's meal in the grave!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

 but it still doesn't work

There is no point pasting three platefuls of spaghetti then just saying "it does not work". If this code were modular you could isolate and test each part in turn to localize and identify where behaviour is not following design. As it is you have a main function with 128 lines and no one but you really knows what is going on so I doubt anyone here is magically going to spot the flaw in the logic. The lack of comments does not help.

 

People could comment on isolated parts of the code. For example I could suggest that a line such as:

							OCR1B = ICR1 - (isetp/5)*ICR1;

looks "dangerous" because, in integer maths, you are doing divide before multiply. OTOH is you do multiply before divide (usually safer) you do have to watch out for overflow and possibly cast  up to wide enough types.

 

BTW do not go back and edit code in previous posts. If something changes post the new version or just post a note to say what has changed - as it stands now the comments in this thread no longer match the code now present in #1

 

Oh and don't make every variable in a program "voltatile". Only apply volatile to the variables you know must be volatile.

Last Edited: Tue. Nov 5, 2019 - 03:54 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

OK Sir. Noted

and i will add the comments to the #1 code so that people can follow my logic

Humility, O humans. We are just a worm's meal in the grave!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

AhmedHamdy wrote:
i will add the comments to the #1 code

Again, don't edit a post after people have replied to it!

 

Post updates as a reply.

 

Also, when quoting, please don't quote the entire original post - it just clogs up the thread with unnecessary repetition, and makes it hard to follow.

Just pick out the necessary part(s) for context - as other posters here have done.

 

EDIT

 

When your reply is just a general response to an entire post, you can  use the 'Reply' button - and the forum automatically identifies the post you're replying to:

 

 

 

https://www.avrfreaks.net/commen...

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Tue. Nov 5, 2019 - 05:08 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
If this code were modular you could isolate and test each part in turn to localize and identify where behaviour is not following design.

Absolutely!

 

@ AhmedHamdy - hence my suggestion that you start with the simple stuff first.

 

Get LEDs and UART output working first.

 

You can then use these to instrument further code as you add it - like clawson says, in a modular fashion.

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

 

This is a completely ridiculous & useless part of a schematic...why would anyone draw such a thing like this?  TERRIBLE 

 

 

Are we supposed to search all around the schematic to figure out what this is about??  It should be shown connected to OnoFF  and/or I7 

Or when looking at the other portion have to search all around for THIS little part to figure out what is going on?

Why even consider drawing such a ridiculous thing?  On top of that, why is the 5V sideways & the transistor strangely on its side?

 

Also, ground always points downward...half of them are sideways, upside down left , right, etc.   Never seen a decent schematic?

Maybe redraw it as round 2 and it will be much better (since in round 2 will have a better idea of what needs drawn). 

 

 

This is an invalid way to draw..every part must connect to a WIRE that connects to any other parts.

In some packages this wouldn't even register as a connection.

Also, any wire should come straight out from the part symbol, not 90 degrees at the edge of the symbol

 

 

Why is this sideways?  Take time to do things properly!!

In a hurry?  Haste makes waste in design and software coding.

 

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Tue. Nov 5, 2019 - 05:25 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

(never mind).

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Tue. Nov 5, 2019 - 05:25 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I was just idly reading this code trying to work out what is supposed to be going on (the lack of comments means I have no choice) and I can't help wondering if things like:

			if(devicestatus == 0) //check if device condition is idle
			{
				devicestatus = 1; //set device condition to ON
			} else if(devicestatus == 1) //check if device condition is already ON
			 {
				devicestatus = 2; //set device condition to OFF
			 } else {
				 devicestatus = 0; //set device condition to idle
			 }

might be easier as:

              if (++devicestatus > 2) {
                  devicestatus = 0;
              }

if you are going to use 0=idle, 1=on, 2=off then at least do something like:

typedef enum {
    IDLE = 0,
    ON = 1,
    OFF = 2
} devstat_e;

devstat_e deviceStatus = IDLE;

if (deviceStatus == IDLE) {
    deviceStatus = ON;
}
else if (deviceStatus == ON) {
    deviceStatus = OFF;
}
else {
    deviceStatus = IDLE;
}

because this code "speaks for itself" - it's clear to the reader what is going on here.

 

Then in the switch() you have (effectively) :

switch(deviceStatus) {
    case 0: blah;
    case 2: blah;
    case 1: blah;
}

I know it does not matter but convention kind of suggests you would put these in ascending numeric order if just using numbers:

switch(deviceStatus) {
    case 0: blah;
    case 1: blah;
    case 2: blah;
}

of course if you go with an enum I guess it doesn't matter so much if it is:

switch(deviceStatus) {
    case IDLE: blah;
    case OFF: blah;
    case ON: blah;
}

because the reader is not immediately aware that is really 0,2,1 not 0,1,2.

 

The thing I have issue with really is how much "stuff" is in case 1 (ON). It makes it tricky to follow the logic - surely what's been done there can be grouped into just a handful of functions with descriptive names? For example the whole switch(global) could probably be broken off into a separate "processGlobalState()" or something? The idea being to leave the main/while/switch just showing the headline summary of what is being done rather than including all the intricate detail in the one place.

 

This doesn't just matter now for you inviting others (us) to read and try to follow the logic of this code but you will be in the same position as us in 3 years time when you come back to this to fix or improve it and you have to try and re-learn what the overall design pattern was.

 

BTW coming back to the actual problem report in #1:

 But I dunno why my while(1) loop doesn't start.

I'd start by asking what makes you think it "doesn't start" ? The only way it could "not start" is if in:

int main(void)
{
	sei(); // Enable Interrupt
	InitializeMrLCD();	//initialization LCD
	InitADC(); //initialization of ADC
	Enable_Interface(); // Setting bits for buttons and leds

    while(1)

either InitializeMrLCD() (stupid function name by the way - no point in anthropomorphising!) or InitADC() or Enable_Interface() never returned. If we can assume that InitializMrLCD() does succeed and return and that there is an active LCD then surely you can use:

int main(void)
{
    sei(); // Enable Interrupt
    InitializeMrLCD();	//initialization LCD
    Send_A_String("Now calling InitADC");
    InitADC(); //initialization of ADC
    Send_A_String("Now calling Enable_Interface");
    Enable_Interface(); // Setting bits for buttons and leds
    Send_A_String("Now starting while(1)");
    while(1)

If it's really the case that the while() loop does not start the last thing printed on the LCD should show where it went before it got "stuck".

 

You said you didn't have a debug interface - but you do really - the LCD should allow you to trace the logical flow in this code (and report key variables and so on). The only thing about LCD debugging is that it is a bit tedious compared to an ICE as you have to guess what it is you need to check next and put in suitable print() statements for that before you start to debug.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


As  avrcandies  says, the schematic is really hard to read & follow.

 

In a schematic, there is no need or reason to have different symbols for SMD components and other types of the same component:

  

 

At the level of a schematic, a resistor is a resistor - all resistors should have the same symbol.

 

In your component values, remember that case matters

  • pF - not PF
  • nF - not NF

 

Have consistent identifiers for the components

  • R for resistors
  • C for capacitors etc

 

Not:

   or  and certainly not 

 

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Check out this thread for a whole load of great tips on how to make provision to debug a board design (including its software):

 

https://www.avrfreaks.net/forum/how-do-i-check-life-signs-scope-atmega2560-standalone?skey=designfordebug

 

This is especially important when you don't have a debug adaptor!

 

awneil wrote:
Using the UART to send debug messages

Oh - I see you have used-up the UART pins for something else.

 

That is a really poor design decision - as a UART must be (one of) the best ways to debug your code without a proper debugger.

 

clawson wrote:
the LCD should allow you to trace the logical flow in this code (and report key variables and so on).

Indeed - but first you have to get the LCD code debugged & working!

 

Plus, on an LCD you will need to add delays to make sure that messages stay visible for long enough to be seen.

And you have no way to capture a "stream" of messages.

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Tue. Nov 5, 2019 - 06:00 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


 

 

both of these are improper

Some amount of wire should emit from the part, THEN you can form a connection or make a 90 deg turn

Also never connect two crossover wires with a dot...if the dot is hard to see, due to monochrome printing, copying or reproduction, the interpretation will be greatly affected.

 

 

 

 

WHAT is this...is this some sort of joke?

 

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Tue. Nov 5, 2019 - 06:53 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

First thing to do is dump mrlcd, and use peter flurry’s lcd code, peters i2c and USART code is a must have as well so get all three.  
now write small test code to get the lcd working,

then add functions to pole your buttons, then PWM, then i2c port expander, etc....

one function at a time, debugging as you go along.

As has already been stated, start with a fresh schematic, and a written design document before coding.

come back when you have these documents and we can than help you get this project working when you get stuck.

good luck,

jim

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274
get $5 free gold/silver https://www.onegold.com/join/713...

 

 

 

 

Last Edited: Tue. Nov 5, 2019 - 06:56 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

OK i will do it again from scratch.

And appreciate all your notes.

I know my C coding is dumb and I will try to improve it

And I take all your notes on account

Give me time and i will come back with better one.

 

Best regards

Humility, O humans. We are just a worm's meal in the grave!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Everything will be 10 times better the next round---so you will be glad you take the time to do it & pave over any previous potholes.

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

It's not unusual to build a prototype first then re-use parts of that design for the final implementation and that goes as much for software as hardware.

 

When you do re-implement I would approach the design in a much more modular fashion. With that in mind I would get the LCD working first on it's own. Then as you add functions to implement the main part of the task use the LCD library heavily to show test results from each part of the code. That way you will be confident that each "building block" works well in isolation. When you have a number of well tested building blocks all that remains is to put them together in main code that "glues" the modules inputs and outputs together.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

As clawson just said (and I also said earlier), you really need to build this in a modular, step-by-step approach.

 

As you don't have a debugger, it is essential that you must provide debug features in your own design.

 

That means both hardware features, and software features.

 

clawson has suggested using the LCD.

 

I would most strongly suggest that you make a UART connection available to output your debug messages - as explained in #27, this will be far & away more "comfortable" than using the LCD.

 

So I would re-phrase the advice as:

I would get the UART working first, on its own.

Then as you add functions to implement the main part of the task use the UART library heavily to show progress & test results from each part of the code.

That way you will be confident that each "building block" works well in isolation. 

Again, having some LEDs and/or uncommitted IO pins available for debug use is also invaluable.

 

And again, that thread with debug tips:

 

https://www.avrfreaks.net/forum/...

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I added a test function which turn led on and off after every procedure in my code, And i started to debug the whole code results:

I found that the code stuck at the while loop of the following function:

void Check_IF_MrLCD_isBusy()
{
	DataDir_MrLCDsCrib = 0;
	MrLCDsControl |= 1<<ReadWrite;
	MrLCDsControl &= ~1<<BiPolarMood;
    testled();
		while (MrLCDsCrib >= 0x80)
		{
			Peek_A_Boo();
		}
    testled();
	DataDir_MrLCDsCrib = 0xFF; //0xFF means 0b11111111
}

  i started to use multimeter to debug why is the BF of my LCD is staying High. And it turns out that after initializing the RW high. It goes high and stay high for a second then it goes low again. So i started to inspect my hardware. And checking why this Pin goes low after setting it high. and till now i dunno why.

But i found a very bad design in my schematic which is the following:

Shorted Pin to GND with no resistor  

A SHORTED PIN TO GND WITH NO PULL DOWN RESISTOR....OMG

 

so i think i fried my ATMEGA16 because the CLIM pin is always high even before it's initialization.

So i will buy a new ATMEGA today and try it after modifying my circuit and added a resistor between the CLIM pin of my ATMEGA and the Q3.

Humility, O humans. We are just a worm's meal in the grave!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1
		while (MrLCDsCrib >= 0x80)
		{
			Peek_A_Boo();
		}

When you first downloaded this LCD driver code and read this did you not think "this looks like utter nonsense" ??

 

We've seen "MrLCD" here on Freaks before. It does not inspire confidence.

 

Most readers here, who want reliable and well written LCD support use this:

 

http://homepage.hispeed.ch/peterfleury/avr-lcd44780.html

 

with the code here:

 

http://homepage.hispeed.ch/peter...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

 

WHAT are you talking about?  Do you have an idea of what you are doing?  

How is the pin pulled high?  Where do you see any short?

 

Clim input is either floating* (transistor off), or intentionally grounded (transistor on).

          *Clim will get pulled hi if you have the AVR pullup turned on 

 

Why is clim sideways?????!!!?

The DOT really belongs on the wire not right on top of the gnd symbol

....if for some reason the wire does not actually make it to the gnd symbol, you won't be able to see the mistake, because it is covered up by the dot.

Then you will really wonder why the circuit doesn't work!

 

 

 

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Wed. Nov 6, 2019 - 05:41 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

AhmedHamdy wrote:
i think i fried my ATMEGA16

Now you see why I asked if your hardware was actually known to be working?

 

 i started to use multimeter to debug

A multimeter is not a very effective debugging tool for microcontroller stuff - it will not show you any "rapid" variations, noise, etc ...

 

So i will buy a new ATMEGA today and try it after modifying my circui...

I thought we'd agreed that you need to start again from scratch - not just modify the existing stuff ?

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

awneil, take a deep breath. you always refer people to your signature tips like its something holy sacred but your way of communicating with the new users of the forum I find it quite bad and not respectable. the first 7 - 8 comments from yourside is full of somehow attacking and not discussing at all the problem of the thread itself.

 

your signature tips are nice, but its not something special am sorry.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If its not for the other members of the forum that came in to help, this behaviour is going to reach 100 useless posts.