Avr output go down

Thread Starter

mah

Joined Mar 15, 2010
393
I built program on atmega8 and i simulated it on proteus and it worked without coontecting any thing to the output of avr. When i connect the output as led. the microcontroller output go low and the led doesnt turn on. Where is the problem?
 

AlbertHall

Joined Jun 4, 2014
12,625
I built program on atmega8 and i simulated it on proteus and it worked without coontecting any thing to the output of avr. When i connect the output as led. the microcontroller output go low and the led doesnt turn on. Where is the problem?
Did you have a current limiting resistor in series with the LED, say 470Ω ?
 

Thread Starter

mah

Joined Mar 15, 2010
393
Did you have a current limiting resistor in series with the LED, say 470Ω ?
Yes,there is and i tried to put larger value resistance but it is still low. If you know proteus the outputs of microconyroller is highlighted when the output is high. In my case nothing work when i put load
 

Papabravo

Joined Feb 24, 2006
22,082
I built program on atmega8 and i simulated it on proteus and it worked without coontecting any thing to the output of avr. When i connect the output as led. the microcontroller output go low and the led doesnt turn on. Where is the problem?
There are several possibilities:
  1. Your output is actually configured as an input and the external load is forcing it low.
  2. Your output is configured as an open-drain which requires an external pullup
  3. Your output is correctly configured as an output, but the value in the data register is a zero.
  4. You have executed a read-modify-write construction which has resulted in the output being set low.
  5. EDIT: you've configured the output as a tri-state, high impedance pin.
 
Last edited:

Thread Starter

mah

Joined Mar 15, 2010
393
here is the code
C:
/*****************************************************
Project :
Date    : 19/06/2017
Author  : mado
chip type               : ATmega8
Program type            : Application
AVR Core Clock frequency: 8.000000 MHz
Memory model            : Small
External RAM size       : 0
Data Stack size         : 256
*****************************************************/

#include <mega8.h>
#include <stdint.h>
#include <stdio.h>
#include <delay.h>
#include <stdlib.h>

#define DHT11       //define DHT22 or DHT11

#define DHT_PORT        PORTC
#define DHT_DDR         DDRC
#define DHT_PIN         PINC
#define DHT_BIT         2
#define ADC_VREF_TYPE 0x00

#asm
   .equ __lcd_port=0x12 //PORTD      RS,RW,E
#endasm
// Alphanumeric LCD Module functions
#include <alcd.h>
// Read the AD conversion result
unsigned int read_adc(unsigned char adc_input)
{
ADMUX=adc_input | (ADC_VREF_TYPE & 0xff);
// Delay needed for the stabilization of the ADC input voltage
delay_us(10);
// Start the AD conversion
ADCSRA|=0x40;
// Wait for the AD conversion to complete
while ((ADCSRA & 0x10)==0);
ADCSRA|=0x10;
return ADCW;
}
     ///////////// adc
float delta,adc;
char str[16]; //allocate on lcd
    int currentValue ;
  float Tref=36;
//// humidity and temp
unsigned char buff [16];
float h=0;
float t=0;
bit hum=0;
uint8_t data[5]={0,0,0,0,0};
   //////////////////////////////////////////////////////////////////////////////////////////
int read_dht_hum()
{

unsigned char i,j;

//=============MCU send START
DHT_DDR|=(1<<DHT_BIT); //pin as output
DHT_PORT&=~(1<<DHT_BIT); //0
delay_ms(18);
DHT_PORT|=(1<<DHT_BIT); //1
DHT_DDR&=~(1<<DHT_BIT); //pin as input
//=============check DHT11 response
delay_us(50);
if (DHT_PIN&(1<<DHT_BIT))
    {
    return 0;
    }
delay_us(80);
if (!(DHT_PIN&(1<<DHT_BIT)))
    {
    return 0;
    }
//===============receive 40 data bits
while (DHT_PIN&(1<<DHT_BIT));
for (j=0; j<5; j++)
    {
    data[j]=0;
    for(i=0; i<8; i++)
        {
        while (!(DHT_PIN&(1<<DHT_BIT)));
        delay_us (30);
        if (DHT_PIN&(1<<DHT_BIT))
            data[j]|=1<<(7-i);
        while (DHT_PIN&(1<<DHT_BIT));
        }
    }

return 1;

}

void dht_read()
{
   hum=0;
       #asm("cli")
        hum = read_dht_hum();
       #asm("sei")
    if(hum==1)
    {
      #ifdef DHT22
     h = (float) data[0] * 256 + data[1];
     h = h/10;
     t = (float) data[2] * 256 + data[3];
     t = t/10;
     #else
     h =  data[0];
     t =  data[2];
     #endif
     }
}


void pr()
{
   dht_read();
 

   lcd_gotoxy(0,1);        
   #ifdef DHT22
   sprintf(buff,"DHT22 %.1f%C/%.1f%%  ",t,h);  //DHT22
   #else
   sprintf(buff,"H=%.1f%% ",h);//DHT11  //"T=%.1f C

    #endif
       lcd_puts(buff);
       //////////////////////read temperature analog
      currentValue = read_adc(0);
     lcd_gotoxy(8,1);
    lcd_putsf("T=");
    lcd_gotoxy(10,1);
    adc=currentValue*0.4887;//  (500)/1023;
    ftoa(adc,0x02, str);
    lcd_puts(str);
    lcd_gotoxy(14,1);
    lcd_putchar(0xdf);        //degree symbol
    lcd_putsf("C");
} 

// Timer 2 output compare interrupt service routine
interrupt [TIM2_COMP] void timer2_comp_isr(void)
{
// Place your code here
 
}

// External Interrupt 0 service routine
interrupt [EXT_INT0] void ext_int0_isr(void)
{

    }

// External Interrupt 1 service routine
interrupt [EXT_INT1] void ext_int1_isr(void)
{

    lcd_init(16);
    lcd_clear();
    lcd_gotoxy(5,1);
    lcd_putsf("Tref=");
    lcd_gotoxy(10,1);
    ftoa(Tref,0x02, str);
    lcd_puts(str);
    lcd_gotoxy(14,1);
    lcd_putchar(0xdf);        //degree symbol
    lcd_putsf("C");
    
    Tref=Tref+0.1;
    if(Tref>=38)
    {
    Tref=36;
    }

        delta=(adc-Tref);

    delay_ms(1000);
   }

/////////////////////////////////
void main(void)
{
PORTB=0x00;
DDRB=0xF0;

PORTC=0x;
DDRC=0xFF;

PORTD=0x00;
DDRD=0xE0;

TCCR0=0x00;
TCNT0=0x00;

TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: TOSC1 pin
// Clock value: PCK2/1024
// Mode: CTC top=OCR2
// OC2 output: Disconnected
ASSR=0x08;
TCCR2=0x0F;
TCNT2=0x00;
OCR2=0x20;

// External Interrupt(s) initialization
// INT0: On
// INT0 Mode: ANY CHANGE
// INT1: On
// INT1 Mode: ANY CHANGE
GICR|=0xC0;
MCUCR=0x00;
GIFR=0xC0;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x80;
// USART initialization
// USART disabled
UCSRB=0x00;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

// SPI initialization
// SPI disabled
SPCR=0x00;

// TWI initialization
// TWI disabled
TWCR=0x00;
//////////////
// ADC initialization
// ADC Clock frequency: 1000.000 kHz
// ADC Voltage Reference: AREF pin
ADMUX=ADC_VREF_TYPE & 0xff;
ADCSRA=0x83;
////////////////////
// Alphanumeric LCD initialization
// Connections are specified in the
// Project|Configure|C Compiler|Libraries|Alphanumeric LCD menu:
// RS - PORTD Bit 5
// RD - PORTD Bit 6
// EN - PORTD Bit 7
// D4 - PORTB Bit 2
// D5 - PORTB Bit 3
// D6 - PORTB Bit 4
// D7 - PORTB Bit 5
// Characters/line: 8
lcd_init(16);
lcd_clear();

// Global enable interrupts
#asm("sei")

while (1)
      {
 
             
    delta=(adc-Tref);
    if  ( adc >=Tref && delta>=0.3)//
    {
    PORTB.1=0;  //turn heater off
    }
             

    else if (adc <=Tref && delta<=-0.3)//
    {

    PORTB.1=1 ; //turn heater on
    }    


    if(adc<=35)    //if temperature <35 alarm
    {
    PORTD.1=1;  //alarm
    }
    else
    PORTD.1=0;  //alarm

        pr();
 
      }

}
and the proteus picture pin 15 (PB1) is high however , the relay is not activated. if i disconnected it it goes high as in the picture
 

Attachments

Papabravo

Joined Feb 24, 2006
22,082
here is the code
C:
/*****************************************************
Project :
Date    : 19/06/2017
Author  : mado
chip type               : ATmega8
Program type            : Application
AVR Core Clock frequency: 8.000000 MHz
Memory model            : Small
External RAM size       : 0
Data Stack size         : 256
*****************************************************/

#include <mega8.h>
#include <stdint.h>
#include <stdio.h>
#include <delay.h>
#include <stdlib.h>

#define DHT11       //define DHT22 or DHT11

#define DHT_PORT        PORTC
#define DHT_DDR         DDRC
#define DHT_PIN         PINC
#define DHT_BIT         2
#define ADC_VREF_TYPE 0x00

#asm
   .equ __lcd_port=0x12 //PORTD      RS,RW,E
#endasm
// Alphanumeric LCD Module functions
#include <alcd.h>
// Read the AD conversion result
unsigned int read_adc(unsigned char adc_input)
{
ADMUX=adc_input | (ADC_VREF_TYPE & 0xff);
// Delay needed for the stabilization of the ADC input voltage
delay_us(10);
// Start the AD conversion
ADCSRA|=0x40;
// Wait for the AD conversion to complete
while ((ADCSRA & 0x10)==0);
ADCSRA|=0x10;
return ADCW;
}
     ///////////// adc
float delta,adc;
char str[16]; //allocate on lcd
    int currentValue ;
  float Tref=36;
//// humidity and temp
unsigned char buff [16];
float h=0;
float t=0;
bit hum=0;
uint8_t data[5]={0,0,0,0,0};
   //////////////////////////////////////////////////////////////////////////////////////////
int read_dht_hum()
{

unsigned char i,j;

//=============MCU send START
DHT_DDR|=(1<<DHT_BIT); //pin as output
DHT_PORT&=~(1<<DHT_BIT); //0
delay_ms(18);
DHT_PORT|=(1<<DHT_BIT); //1
DHT_DDR&=~(1<<DHT_BIT); //pin as input
//=============check DHT11 response
delay_us(50);
if (DHT_PIN&(1<<DHT_BIT))
    {
    return 0;
    }
delay_us(80);
if (!(DHT_PIN&(1<<DHT_BIT)))
    {
    return 0;
    }
//===============receive 40 data bits
while (DHT_PIN&(1<<DHT_BIT));
for (j=0; j<5; j++)
    {
    data[j]=0;
    for(i=0; i<8; i++)
        {
        while (!(DHT_PIN&(1<<DHT_BIT)));
        delay_us (30);
        if (DHT_PIN&(1<<DHT_BIT))
            data[j]|=1<<(7-i);
        while (DHT_PIN&(1<<DHT_BIT));
        }
    }

return 1;

}

void dht_read()
{
   hum=0;
       #asm("cli")
        hum = read_dht_hum();
       #asm("sei")
    if(hum==1)
    {
      #ifdef DHT22
     h = (float) data[0] * 256 + data[1];
     h = h/10;
     t = (float) data[2] * 256 + data[3];
     t = t/10;
     #else
     h =  data[0];
     t =  data[2];
     #endif
     }
}


void pr()
{
   dht_read();


   lcd_gotoxy(0,1);      
   #ifdef DHT22
   sprintf(buff,"DHT22 %.1f%C/%.1f%%  ",t,h);  //DHT22
   #else
   sprintf(buff,"H=%.1f%% ",h);//DHT11  //"T=%.1f C

    #endif
       lcd_puts(buff);
       //////////////////////read temperature analog
      currentValue = read_adc(0);
     lcd_gotoxy(8,1);
    lcd_putsf("T=");
    lcd_gotoxy(10,1);
    adc=currentValue*0.4887;//  (500)/1023;
    ftoa(adc,0x02, str);
    lcd_puts(str);
    lcd_gotoxy(14,1);
    lcd_putchar(0xdf);        //degree symbol
    lcd_putsf("C");
}

// Timer 2 output compare interrupt service routine
interrupt [TIM2_COMP] void timer2_comp_isr(void)
{
// Place your code here

}

// External Interrupt 0 service routine
interrupt [EXT_INT0] void ext_int0_isr(void)
{

    }

// External Interrupt 1 service routine
interrupt [EXT_INT1] void ext_int1_isr(void)
{

    lcd_init(16);
    lcd_clear();
    lcd_gotoxy(5,1);
    lcd_putsf("Tref=");
    lcd_gotoxy(10,1);
    ftoa(Tref,0x02, str);
    lcd_puts(str);
    lcd_gotoxy(14,1);
    lcd_putchar(0xdf);        //degree symbol
    lcd_putsf("C");
  
    Tref=Tref+0.1;
    if(Tref>=38)
    {
    Tref=36;
    }

        delta=(adc-Tref);

    delay_ms(1000);
   }

/////////////////////////////////
void main(void)
{
PORTB=0x00;
DDRB=0xF0;

PORTC=0x;
DDRC=0xFF;

PORTD=0x00;
DDRD=0xE0;

TCCR0=0x00;
TCNT0=0x00;

TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: TOSC1 pin
// Clock value: PCK2/1024
// Mode: CTC top=OCR2
// OC2 output: Disconnected
ASSR=0x08;
TCCR2=0x0F;
TCNT2=0x00;
OCR2=0x20;

// External Interrupt(s) initialization
// INT0: On
// INT0 Mode: ANY CHANGE
// INT1: On
// INT1 Mode: ANY CHANGE
GICR|=0xC0;
MCUCR=0x00;
GIFR=0xC0;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x80;
// USART initialization
// USART disabled
UCSRB=0x00;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

// SPI initialization
// SPI disabled
SPCR=0x00;

// TWI initialization
// TWI disabled
TWCR=0x00;
//////////////
// ADC initialization
// ADC Clock frequency: 1000.000 kHz
// ADC Voltage Reference: AREF pin
ADMUX=ADC_VREF_TYPE & 0xff;
ADCSRA=0x83;
////////////////////
// Alphanumeric LCD initialization
// Connections are specified in the
// Project|Configure|C Compiler|Libraries|Alphanumeric LCD menu:
// RS - PORTD Bit 5
// RD - PORTD Bit 6
// EN - PORTD Bit 7
// D4 - PORTB Bit 2
// D5 - PORTB Bit 3
// D6 - PORTB Bit 4
// D7 - PORTB Bit 5
// Characters/line: 8
lcd_init(16);
lcd_clear();

// Global enable interrupts
#asm("sei")

while (1)
      {

           
    delta=(adc-Tref);
    if  ( adc >=Tref && delta>=0.3)//
    {
    PORTB.1=0;  //turn heater off
    }
           

    else if (adc <=Tref && delta<=-0.3)//
    {

    PORTB.1=1 ; //turn heater on
    }  


    if(adc<=35)    //if temperature <35 alarm
    {
    PORTD.1=1;  //alarm
    }
    else
    PORTD.1=0;  //alarm

        pr();

      }

}
and the proteus picture pin 15 (PB1) is high however , the relay is not activated. if i disconnected it it goes high as in the picture
Mystery solved! Consult the Atmel datasheet and you will find that you have onfigured PORTB.1 as a tri-state output. In this condition it cannot drive R3 which is connected to the base of the transistor.
http://www.atmel.com/Images/Atmel-2486-8-bit-AVR-microcontroller-ATmega8_L_datasheet.pdf p.53
 
Top