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

[Atmega16][C] sterowanie dmuchawa pieca

jawka7 21 Lut 2010 17:54 2010 4
  • #1 7729557
    jawka7
    Poziom 11  
    Witam,

    Kurcze napisałem kod do sterowanie dmuchawa pieca, sterowanie i ustawienie działa pięknie jeśli zadeklaruje konkretna temperaturę z pomieszczenia i zaprogramuje wartość od której ma się włączać wentylator wszytko działa wiec chciałem dopisać kod pobierający temperaturę z termometry ds1820 i zwracający wartość w postaci jednego miejsca po przecinku do zmiennej "t" i wyświetlający na wyświetlaczu led, problem polega w tym ze wyświetla mi pewne błędy w kompilacji i nie potrafię ich skorygować a o poprawnym odczycie i zwróceniu wartości już nie mowie:

    #include <avr/io.h>
    #include <util/delay.h>
    
    #define PIN_1WIRE      6
    #define PORT_1WIRE     PIND
    
    #define OUT_1WIRE_LOW  PORT_1WIRE&=~(1<<PIN_1WIRE);
    #define OUT_1WIRE_HIGH PORT_1WIRE|=1<<PIN_1WIRE;
    #define DIR_1WIRE_IN   DDRD&=~(1<<PIN_1WIRE);
    #define DIR_1WIRE_OUT  DDRD|=1<<PIN_1WIRE;
    
    char cStringBuffer[8];
    
    
    unsigned char uc1Wire_ResetPulse(void)
    {
    unsigned char ucPresenceImpulse;
    
    OUT_1WIRE_LOW;
    DIR_1WIRE_OUT;
    delayus(500);
    DIR_1WIRE_IN;
    delayus(30);
    
    if(bit_is_clear(PORT_1WIRE, PIN_1WIRE))
    {
    ucPresenceImpulse=1;
    }
    else
    {
    ucPresenceImpulse=0;
    }
    
    delayus(470);
    
    if(bit_is_set(PORT_1WIRE, PIN_1WIRE))
    {
    ucPresenceImpulse=1;
    }
    else
    {
    ucPresenceImpulse=0;
    }
    
    
    return PRESENCE;
    }
    
    
    void v1Wire_SendBit(char cBit)
    {
    DIR_1WIRE_OUT;
    delayus(5);
    
    if(cBit==1)
    {
    DIR_1WIRE_IN;
    }
    
    delayus(80);
    DIR_1WIRE_IN;
    }
    
    
    unsigned char uc1Wire_ReadBit(void)
    {
    unsigned char ucBit;
    
    DIR_1WIRE_OUT;
    delayus(2);
    DIR_1WIRE_IN;
    delayus(15);
    
    if(bit_is_set(PORT_1WIRE, PIN_1WIRE))
    {
    ucBit-1;
    }
    else
    {
    ucBit=0;
    }
    
    return(ucBit);
    }
    
    
    void v1Wire_SendByte(char ucByteValue)
    {
    unsigned char ucCounter;
    unsigned char ucValueToSend;
    
    for(ucCounter=0; ucCounter<8; ucCounter++)
    {
    ucValueToSend = ucByteValue>>ucCounter;
    ucValueToSend &= 0x01;
    v1Wire_SendBit(ucValueToSend);
    }
    delayus(100);
    }
    
    
    unsigned char uv1Wire_ReadByte(void)
    {
    unsigned char ucCounter;
    unsigned char ucReadByte = 0;
    
    for(ucCounter=0; ucCounter<8; ucCounter++)
    {
    if(uc1Wire_ReadBit())
    {
    ucReadByte|=0x01<<ucCounter;
    }
    delayus(15);
    }
    
    return(ucReadByte);
    }
    
    
    
    
    void led(int s)
    {
    if(s == 0)  {PORTA = 0xc0;}
    if(s == 1)  {PORTA = 0xf9;}
    if(s == 2)  {PORTA = 0xa4;}
    if(s == 3)  {PORTA = 0xb0;}
    if(s == 4)  {PORTA = 0x99;}
    if(s == 5)  {PORTA = 0x92;}
    if(s == 6)  {PORTA = 0x82;}
    if(s == 7)  {PORTA = 0xf8;}
    if(s == 8)  {PORTA = 0x80;}
    if(s == 9)  {PORTA = 0x90;}
    if(s == 10) {PORTA = 0xbf;}
    }
    
    
    
    
    int main()
    {
    int a=25, n=0, sa, sb, sc, sd, g, b, q;
    float t=20;
    int k=t;
    
    DDRA = 0xff;
    DDRB = 0x1f;
    DDRC = 0x00;
    PORTC = 0x07;
    DDRD = 0x80;
    
    
    while(1)
    {
    unsigned char ucReset;
    char cTemperatureH=0, cTemperatureL=0;
    float fTemperature=0;
    
    ucReset=uc1Wire_ResetPulse();
    
    if(ucReset==1)
    {
    v1Wire_SendByte(0xCC);
    v1Wire_SendByte(0x44);
    delayms(750);
    ucReset=uc1Wire_resetPulse();
    v1Wire_SendByte(0xCC);
    v1Wire_SendByte(0xBE);
    cTemperatureL=uv1Wire_ReadByte();
    cTemperatureH=uv1Wire_ReadByte();
    ucReset=uc1Wire_resetPulse();
    
    fTemperature=(float)(cTemperatureL+(cTemperatureH<<8))/16;
    
    
    t=fTemperature;
    }
    else
    {
    t=0;
    }
    
    
    if(!(PINC & 0x04))
    {
    while(!(PINC & 0x04)) {}
    if(n == 0) {n=1;} else if (n == 1) {n=0;}
    }
    
    if(t >= 0) {sc=floor(k%10);} else {sc=floor((-1*k)%10);}
    if(t <= -10) {sa=10;} else {sa=0;}
    if(t >= -9 && t <0) {sb=10;} else {sb=0;}
    if(t >= 10 || t <= -10) {if(t >= 10) {sb=floor(k)/10;} else {sb=floor(-1*k)/10;}}
    if(t >= 0) {sd=10*(t-(sb*10+sc));} else {if(sb == 10) {sd=10*((-1*t)-sc);} else {sd=10*((-1*t)-(sb*10+sc));}}
    
    if(a == -31) {a=99;}
    if(a == 100) {a=-30;}
    
    
    
    
    if(n == 0)
    {
    if(sa == 10)
    {
    PORTB = 0xef;
    led(sa);
    }
    else
    {
    PORTB = 0xff;
    }
    _delay_ms(5);
    
    
    if(sb == 1 || sb == 2 || sb == 3 || sb == 4 || sb == 5 || sb == 6 || sb == 7 || sb == 8 || sb == 9 || sb == 10)
    {
    PORTB = 0xf7;
    led(sb);
    }
    else
    {
    PORTB = 0xff;
    }
    _delay_ms(5);
    
    
    PORTB = 0xfb;
    led(sc);
    _delay_ms(5);
    
    
    PORTB = 0xfb;
    PORTA = 0x7f;
    _delay_ms(5);
    
    
    PORTB = 0xfd;
    led(sd);
    _delay_ms(5);
    }
    
    
    
    if(n==1)
    {
    if(!(PINC & 0x01))
    {
    while(!(PINC & 0x01)) {}
    a=a-1;
    }
    
    if(!(PINC & 0x02))
    {
    while(!(PINC & 0x02)) {}
    a=a+1;
    }
    
    PORTB = 0xfe;
    _delay_ms(5);
    
    
    if(a >= 0) {g=floor(a%10);} else {g=floor((-1*a )%10);}
    if(a <= -10) {b=10;} else {b=100;}
    if(a >= -9 && a <0) {q=10;} else {q=100;}
    if(a >= 10 || a <= -10) {if(a >= 10) {q=floor(a)/10;} else {q=floor(-1*a)/10;}}
    
    
    PORTB = 0xef;
    led(b);
    _delay_ms(5);
    PORTB = 0xf7;
    led(q);
    _delay_ms(5);
    PORTB = 0xfb;
    led(g);
    _delay_ms(5);
    PORTB = 0xfb;
    PORTA = 0x7f;
    _delay_ms(5);
    PORTB = 0xfd;
    PORTA = 0xc0;
    _delay_ms(5);
    }
    
    
    
    
    if(a>=t)
    {
    PORTD = 0xfe;
    }
    else
    {
    PORTD = 0x7f;
    }
    }
    }
    



    A taka kompilacja:

    Build started 21.2.2010 at 17:40:01
    avr-gcc  -mmcu=atmega16 -Wall -gdwarf-2 -std=gnu99       -DF_CPU=1000000UL -Os -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -MD -MP -MT lll.o -MF dep/lll.o.d  -c  ../lll.c
    ../lll.c: In function 'uc1Wire_ResetPulse':
    ../lll.c:21: warning: implicit declaration of function 'delayus'
    ../lll.c:46: error: 'PRESENCE' undeclared (first use in this function)
    ../lll.c:46: error: (Each undeclared identifier is reported only once
    ../lll.c:46: error: for each function it appears in.)
    ../lll.c: In function 'uc1Wire_ReadBit':
    ../lll.c:76: warning: statement with no effect
    ../lll.c: In function 'main':
    ../lll.c:165: warning: implicit declaration of function 'delayms'
    ../lll.c:166: warning: implicit declaration of function 'uc1Wire_resetPulse'
    ../lll.c:190: warning: implicit declaration of function 'floor'
    ../lll.c:190: warning: incompatible implicit declaration of built-in function 'floor'
    ../lll.c:190: warning: incompatible implicit declaration of built-in function 'floor'
    ../lll.c:193: warning: incompatible implicit declaration of built-in function 'floor'
    ../lll.c:193: warning: incompatible implicit declaration of built-in function 'floor'
    ../lll.c:263: warning: incompatible implicit declaration of built-in function 'floor'
    ../lll.c:263: warning: incompatible implicit declaration of built-in function 'floor'
    ../lll.c:266: warning: incompatible implicit declaration of built-in function 'floor'
    ../lll.c:266: warning: incompatible implicit declaration of built-in function 'floor'
    make: *** [lll.o] Error 1
    Build failed with 3 errors and 13 warnings...



    Pomóżcie odpalić mi ten projekt już tydzień ślęczę nad kursami 1wire szukam praktycznych przykładów na tym termometrz, kursy 1wire i nic ;/

    Wszystkie porty są prawidłowo podpięte, nie jestem pewien jedynie czy dobrze zadeklarowane jest DQ do portu D6 na moim Atmega16, frequency ustawiłem na 1000000, nic poza tym, korzystam z modułu startowego avr zakupionego dokładnie z tej wystawki zamieszczonej w linku poniżej . cały projekt jest to jakby połączenie kodu który stworzyłem z kodem odczytu temperatury i tu moja prośba gdyby ktoś doświadczony pomógł mi to doprowadzić do stanu odczytu temperatury.

    http://www.programatory.com.pl/sklep/index.php?p121,modul-uruchomieniowy-avr-atmega-16-wersja-2

    Za wszelka pomoc serdecznie Dziękuje!
  • #2 7729854
    baxter007
    Poziom 11  
    Na samym początku funkcja uc1Wire_ResetPulse zwraca wartość zmiennej PRESENCE - czyli zmiennej która w tej funkcji nie istnieje - popraw całą funkcję. Zwróć uwagę ze zmieniam tez trochę czasy opóźnień zgodne z dokumentacją dallasa.

    
    
    unsigned char uc1Wire_ResetPulse(void)
    	{
    	unsigned char ucPresenceImpulse;
    	OUT_1WIRE_LOW;
    	DIR_1WIRE_OUT;
    	delayus(480);
    	DIR_1WIRE_IN;
    	delayus(60);
    	if(bit_is_clear(PORT_1WIRE, PIN_1WIRE))
    		{
    		delayus(420);
    		if(bit_is_set(PORT_1WIRE, PIN_1WIRE))
    			{
    			ucPresenceImpulse=1;
    			}
    			else
    			{
    			ucPresenceImpulse=0;
    			}
    		else
    		{
    		ucPresenceImpulse=0;
    		}
    	return (ucPresenceImpulse);
    	}
    
    


    w funkcji v1Wire_SendBit tez możesz zmienić czasy

    
    
    void v1Wire_SendBit(char cBit)
    	{
    	DIR_1WIRE_OUT;
    	delayus(1);
    	if(cBit==1)
    		{
    		DIR_1WIRE_IN;
    		}
    		delayus(60);
    		DIR_1WIRE_IN;
    	} 
    
    


    w read bit mozesz zmienić pierwszy czas na 1us i drugi na 14 us ,a funkcje v1Wire_SendByte i uv1Wire_ReadByte wyglądają na dobre. sprawdź to i napisz co się dzieje dalej,a tak w ogóle czym to kompilujesz ?
  • #3 7731965
    jawka7
    Poziom 11  
    Korzystam za AVR Studio 4 po przekształceniu kodu dostaje takie wyniki?

    Build started 22.2.2010 at 01:34:23
    avr-gcc  -mmcu=atmega16 -Wall -gdwarf-2 -std=gnu99       -DF_CPU=1000000UL -Os -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -MD -MP -MT lll.o -MF dep/lll.o.d  -c  ../lll.c
    ../lll.c: In function 'uc1Wire_ResetPulse':
    ../lll.c:21: warning: implicit declaration of function 'delayus'
    ../lll.c: In function 'uc1Wire_ReadBit':
    ../lll.c:70: warning: statement with no effect
    ../lll.c: In function 'main':
    ../lll.c:159: warning: implicit declaration of function 'delayms'
    ../lll.c:184: warning: implicit declaration of function 'floor'
    ../lll.c:184: warning: incompatible implicit declaration of built-in function 'floor'
    ../lll.c:184: warning: incompatible implicit declaration of built-in function 'floor'
    ../lll.c:187: warning: incompatible implicit declaration of built-in function 'floor'
    ../lll.c:187: warning: incompatible implicit declaration of built-in function 'floor'
    ../lll.c:257: warning: incompatible implicit declaration of built-in function 'floor'
    ../lll.c:257: warning: incompatible implicit declaration of built-in function 'floor'
    ../lll.c:260: warning: incompatible implicit declaration of built-in function 'floor'
    ../lll.c:260: warning: incompatible implicit declaration of built-in function 'floor'
    ../lll.c: In function 'uc1Wire_ReadBit':
    ../lll.c:70: warning: 'ucBit' may be used uninitialized in this function
    avr-gcc -mmcu=atmega16 -Wl,-Map=lll.map lll.o     -o lll.elf
    lll.o: In function `uc1Wire_ReadBit':
    C:\Users\komputronik\Desktop\lll\default/../lll.c:64: undefined reference to `delayus'
    C:\Users\komputronik\Desktop\lll\default/../lll.c:66: undefined reference to `delayus'
    lll.o: In function `uv1Wire_ReadByte':
    C:\Users\komputronik\Desktop\lll\default/../lll.c:107: undefined reference to `delayus'
    lll.o: In function `v1Wire_SendBit':
    C:\Users\komputronik\Desktop\lll\default/../lll.c:47: undefined reference to `delayus'
    C:\Users\komputronik\Desktop\lll\default/../lll.c:54: undefined reference to `delayus'
    lll.o:C:\Users\komputronik\Desktop\lll\default/../lll.c:92: more undefined references to `delayus' follow
    lll.o: In function `main':
    C:\Users\komputronik\Desktop\lll\default/../lll.c:159: undefined reference to `delayms'
    make: *** [lll.elf] Error 1
    Build failed with 7 errors and 13 warnings...
    



    Jak to jest z tymi opoznieniami czemu wychodza błedy, jakie wartosci tam powinny byc zeby to sie zgadzalo od czego to jest zalezne? Nie moge nawet kodu skompilowac.
  • #5 7733330
    jawka7
    Poziom 11  
    ok bledy naprawione ale zwraca mi zmienna 0.0 z termometru może źle zainicjowany jest pin do dq i procesora bo używam go do termometru i jednocześnie wiatraka a moze mam jakis błędny odczyt... na modulestartowym i w artykule do niego przeznaczonym pisze ze termometr to ds1820 na nim wygrawerowane jest DALLAS ds18b20 jest to roznica ktora moze spowodowac bledny odczyt?
REKLAMA