logo elektroda
logo elektroda
X
logo elektroda
REKLAMA
REKLAMA
Adblock/uBlockOrigin/AdGuard mogą powodować znikanie niektórych postów z powodu nowej reguły.

[ATMega8][C] Problem przy uruchamianiu układu

ryjek_ 14 Maj 2009 20:26 1131 6
REKLAMA
  • #1 6530662
    ryjek_
    Poziom 10  
    Witam!
    Mój problem jest następujący: po włączeniu zasilania w układzie, aby zadziałał on poprawnie , muszę go zresetować.
    Bez resetu mikrokontroler wystawia śmieci, które niewiadomo skąd bierze. Normalnie powinien pobrać wartości zapisane w eepromie i je wystawić. Dopóki nie zresetuję procka mam same śmieci na nóżkach.
    Nie mam najmniejszego pomysłu, co może powodować takie zachowanie.

    Z góry dzięki za pomoc.
  • REKLAMA
  • REKLAMA
  • #3 6530802
    ryjek_
    Poziom 10  
    A co według Ciebie jest takiego nieuważnego w moim programie?

    #define F_CPU 8000000UL
    #define BAUD 9600
    #define MYUBRR ((F_CPU/(16L*BAUD))-1)
    
    #include <avr/io.h>
    #include <avr/eeprom.h>
    #include <avr/pgmspace.h> 
    #include <util/delay.h> 
    #include <stdint.h>
    
    uint8_t RCDATA[] = {0,0,0,0};
    volatile uint8_t RECEIVE_DATA;
    volatile uint8_t rcI;
    
    volatile uint8_t A,B,C,D;
    
    static const uint8_t LINES[100][5] PROGMEM = {	{0,0,0,0,0x001},{0,0,0,0,0x002},{0,0,0,0,0x003},{0,0,0,0,0x004},{0,0,0,0,0x005},{0,0,0,0,0x006},{0,0,0,0,0x007},{0,0,0,0,0x008},
    												{0,0,0,1,0x011},{0,0,0,1,0x012},{0,0,0,1,0x013},{0,0,0,1,0x014},{0,0,0,1,0x015},{0,0,0,1,0x016},{0,0,0,1,0x017},{0,0,0,1,0x018},
    												{0,0,1,0,0x021},{0,0,1,0,0x022},{0,0,1,0,0x023},{0,0,1,0,0x024},{0,0,1,0,0x025},{0,0,1,0,0x026},{0,0,1,0,0x027},{0,0,1,0,0x028},
    												{0,0,1,1,0x031},{0,0,1,1,0x032},{0,0,1,1,0x033},{0,0,1,1,0x034},{0,0,1,1,0x035},{0,0,1,1,0x036},{0,0,1,1,0x037},{0,0,1,1,0x038},
    												{0,1,0,0,0x041},{0,1,0,0,0x042},{0,1,0,0,0x043},{0,1,0,0,0x044},{0,1,0,0,0x045},{0,1,0,0,0x046},{0,1,0,0,0x047},{0,1,0,0,0x048},
    												{0,1,0,1,0x051},{0,1,0,1,0x052},{0,1,0,1,0x053},{0,1,0,1,0x054},{0,1,0,1,0x055},{0,1,0,1,0x056},{0,1,0,1,0x057},{0,1,0,1,0x058},
    												{0,1,1,0,0x061},{0,1,1,0,0x062},{0,1,1,0,0x063},{0,1,1,0,0x064},{0,1,1,0,0x065},{0,1,1,0,0x066},{0,1,1,0,0x067},{0,1,1,0,0x068},
    												{0,1,1,1,0x071},{0,1,1,1,0x072},{0,1,1,1,0x073},{0,1,1,1,0x074},{0,1,1,1,0x075},{0,1,1,1,0x076},{0,1,1,1,0x077},{0,1,1,1,0x078},
    												{1,0,0,0,0x081},{1,0,0,0,0x082},{1,0,0,0,0x083},{1,0,0,0,0x084},{1,0,0,0,0x085},{1,0,0,0,0x086},{1,0,0,0,0x087},{1,0,0,0,0x088},
    												{1,0,0,1,0x091},{1,0,0,1,0x092},{1,0,0,1,0x093},{1,0,0,1,0x094},{1,0,0,1,0x095},{1,0,0,1,0x096},{1,0,0,1,0x097},{1,0,0,1,0x098},
    												{1,0,1,0,0x0A1},{1,0,1,0,0x0A2},{1,0,1,0,0x0A3},{1,0,1,0,0x0A4},{1,0,1,0,0x0A5},{1,0,1,0,0x0A6},{1,0,1,0,0x0A7},{1,0,1,0,0x0A8},
    												{1,0,1,1,0x0B1},{1,0,1,1,0x0B2},{1,0,1,1,0x0B3},{1,0,1,1,0x0B4},{1,0,1,1,0x0B5},{1,0,1,1,0x0B6},{1,0,1,1,0x0B7},{1,0,1,1,0x0B8},
    												{1,1,0,0,0x0C1},{1,1,0,0,0x0C2},{1,1,0,0,0x0C3},{1,1,0,0,0x0C4}
    											}; 
    
    
    
    void USART_Transmit( unsigned char data );
    void USART_Init( unsigned int ubrr);
    uint8_t USART_Receive( void );
    void ReceiveFrame();
    void updateBoard(uint8_t);
    void setBoard(void);
    
    
    int main (void)
    {
    
    	USART_Init ( MYUBRR );
    	
    	//***********
    	DDRB = 0xFF;           
    	DDRC = 0xBF;
    	DDRD = 0xFF;
    
    	PORTB=0x00;
    	PORTC=0x40;
    	PORTD=0x00;
    	//***************
    
    	rcI = 0;
    	RECEIVE_DATA = 0;
    
    	_delay_ms(100);
    	setBoard();
    
    	while(1){
    	
    		if(RECEIVE_DATA == 0)	ReceiveFrame();
    
    		else if(RECEIVE_DATA == 1 ){
    		
    			uint8_t lineValue = 0;
    			uint8_t lineCode = 0;
    
    			lineCode = (RCDATA[0] * 100) + (RCDATA[1] * 10) + RCDATA[2] -1;
    			lineValue = RCDATA[3];
    
    			eeprom_busy_wait();
    			eeprom_write_byte( (uint8_t *)pgm_read_byte(&LINES[lineCode][4]), lineValue);
    
    			D = pgm_read_byte(&LINES[lineCode][0]);
    			C = pgm_read_byte(&LINES[lineCode][1]);
    			B = pgm_read_byte(&LINES[lineCode][2]);
    			A = pgm_read_byte(&LINES[lineCode][3]);
    
    			updateBoard(lineCode); 
    			
    			RECEIVE_DATA = 0;
    			rcI = 0;
    
    			USART_Transmit('*');
    			}
    	
    	}
    		
    
    	return 0;
    }
    
    
    void setBoard(){
    
    	volatile uint8_t a,b,c,d,e,f,g,h;
    	uint8_t beginAddress = 0x001;
    	uint8_t j;
    	uint8_t pointer = 0;
    
    
    	while(pointer != 104){
    
    		eeprom_busy_wait();a = eeprom_read_byte((uint8_t *) beginAddress);
    		eeprom_busy_wait();b = eeprom_read_byte((uint8_t *) beginAddress+1);
    		eeprom_busy_wait();c = eeprom_read_byte((uint8_t *) beginAddress+2);
    		eeprom_busy_wait();d = eeprom_read_byte((uint8_t *) beginAddress+3);
    		eeprom_busy_wait();e = eeprom_read_byte((uint8_t *) beginAddress+4);
    		eeprom_busy_wait();f = eeprom_read_byte((uint8_t *) beginAddress+5);
    		eeprom_busy_wait();g = eeprom_read_byte((uint8_t *) beginAddress+6);
    		eeprom_busy_wait();h = eeprom_read_byte((uint8_t *) beginAddress+7);
    		
    		beginAddress += 16;
    		
    		_delay_ms(200);
    		D = pgm_read_byte(&LINES[pointer][0]);
    		C = pgm_read_byte(&LINES[pointer][1]);
    		B = pgm_read_byte(&LINES[pointer][2]);
    		A = pgm_read_byte(&LINES[pointer][3]);
    
    		pointer += 8;
    
    
    		PORTC = (a << 0) | (b << 1) | (c << 2) | (d << 3) | (e << 4) | (f << 5);
    		PORTB = (g << 4) | (h << 5) |(A << 0); 
    
    		PORTD = (B << 7) | (C << 6) | (D << 5); // wybór zatrzasku
    		
    		PORTB &= ~_BV(PB7); // odblokowanie MUXa
    		_delay_ms(50);
    		PORTB |=_BV(PB7);
    		_delay_ms(50);
    
    		PORTC = (0 << 0) | (0 << 1) | (0 << 2) | (0 << 3) | (0 << 4) | (0 << 5);
    		PORTB = (0 << 4) | (0 << 5) | (1 << 7);
    
    
    	}
    
    }
    void updateBoard(uint8_t lineNumber){ 
    
    	volatile uint8_t valueTab[8];
    	uint8_t j,k,i;
    	uint8_t okFlag = 1;
    
    	i = 0;
    	for(j=(lineNumber -9); j<(lineNumber + 9);j++){
    	
    		for(k=0; k<3; k++){
    			if(pgm_read_byte(&LINES[lineNumber][k]) != pgm_read_byte(&LINES[j][k])) okFlag = 0;
    			}
    
    		if(okFlag){
    			eeprom_busy_wait();
    			valueTab[i++] = eeprom_read_byte((uint8_t *)pgm_read_byte(&LINES[j][4]));
    			_delay_ms(200);
    		}
    	}
    
    	D = pgm_read_byte(&LINES[lineNumber][0]);
    	C = pgm_read_byte(&LINES[lineNumber][1]);
    	B = pgm_read_byte(&LINES[lineNumber][2]);
    	A = pgm_read_byte(&LINES[lineNumber][3]);
    
    
    	PORTC = (valueTab[0] << 0) | (valueTab[1] << 1) | (valueTab[2] << 2) | (valueTab[3] << 3) | (valueTab[4] << 4) | (valueTab[5] << 5);
    	PORTB = (valueTab[6] << 4) | (valueTab[7] << 5) |(A << 0); 
    
    	PORTD = (B << 7) | (C << 6) | (D << 5); // wybór zatrzasku
    		
    	PORTB &= ~_BV(PB7); // odblokowanie MUXa
    	_delay_ms(50);
    	PORTB |=_BV(PB7);
    	_delay_ms(50);
    
    	PORTC = (0 << 0) | (0 << 1) | (0 << 2) | (0 << 3) | (0 << 4) | (0 << 5);
    	PORTB = (0 << 4) | (0 << 5)| (1 << 7);
    }
    
    void USART_Init( unsigned int ubrr)
    {
    	/* Set baud rate */
    	UBRRH = (unsigned char)(ubrr>>8);
    	UBRRL = (unsigned char)ubrr;
    	/* Enable receiver and transmitter */
    	UCSRB = (1<<RXEN)|(1<<TXEN);
    	/* Set frame format: 8data, 2stop bit */
    	UCSRC = (1<<URSEL)|(1<<USBS)|(3<<UCSZ0);
    }
    
    
    void USART_Transmit( unsigned char data )
    {
    	/* Wait for empty transmit buffer */
    	while ( !( UCSRA & (1<<UDRE)) )
    	;
    	/* Put data into buffer, sends the data */
    	UDR = data;
    }
    
    
    uint8_t USART_Receive( void )
    {
    	/* Wait for data to be received */
    	while ( !(UCSRA & (1<<RXC)) )
    	;
    	/* Get and return received data from buffer */
    	return UDR;
    }
    
    void ReceiveFrame(){
    
    	RCDATA[rcI] = USART_Receive(); rcI++;
    	
    	if( rcI > 3){
    		rcI = 0;
    		RECEIVE_DATA = 1;
    	}
    
    }
    
  • #4 6530921
    Madergames9
    Poziom 15  
    A może sam start procesorka... jak widać reczny reset dobrze działa, ale podczas załączenia układu powinien się on resetować automatycznie. Skoro sam program dobrze działa po resecie ręcznym to sprawdż czy jest reset podczas startu układu po załączeniu napięcia. Reset inicjacja wyświetlacza również

    Pozdrawiam
  • REKLAMA
  • #5 6531038
    dawid512
    Poziom 32  
    Dodaj rezystor podciągający reset do VCC ok 10kΩ. Możesz także zmienić opóźnienie po starcie w fuse bitach.
  • #6 6531245
    ryjek_
    Poziom 10  
    Reset mam pociągniety do VCC przez rezystor 100 k
  • REKLAMA
  • #7 6595387
    Madergames9
    Poziom 15  
    Zmien rezystor na 10k tak jak wczesniej powiedziano... :D
REKLAMA