Hi,

I'm working on a project using the 18f4520, and i've ran into a problem. It 
seems that passing an array to a function results in garbage being read in the 
receiving function.

It seems as if the receiving function is using the wrong address to read from! 
I've fiddled with the stack, but this didn't seem to influence the problem.
I'm now using the latest SDCC snapshot (2.8.4 #5250 (Oct 12 2008)) and gputils 
0.13.6 on Linux. 
Tried SDCC 2.8.0 with similair results (the garbage output was different, but 
still garbage..)
Also tried to build this on windows XP on another machine, using the same SDCC 
snapshot and gputils version. No change there.

Some code demonstrating this issue is attached below. I've compiled this using

        sdcc -mpic16 -p18f4520 main.c

and loading the resulting main.hex file using and ICD2 programmer from MPLAB 
(on the Windows XP machine).

Output on the uart @ 19200 baud should be "[????]", but I get "[??" followed by 
a lot of garbage

Hope anyone can give me some pointers here!

Thanks in advance,
-Bastiaan van Kesteren

--------------the code:--------------

#include <pic18fregs.h>
#include <usart.h>
#include <stdio.h>

/* Initialise a stack of 255 bytes at RAM address 0x300 */
#pragma stack 0x300 0xFF

/* Baudrate = FOSC/(16(X+1)): 25 = 19200 @ 8MHz */
#define BAUDRATE    25

/* Oscillator */
code char at __CONFIG1H _conf0 = _OSC_INTIO67_1H;

/* Watchdog */
code char at __CONFIG2H _conf2 = _WDT_OFF_2H;

/* Power up timeout and brown out detection enabled */
code char at __CONFIG2L _conf1 = _PWRT_ON_2L & _BOREN_ON_2L;
/* MCLR settings */
code char at __CONFIG3H _conf3 = _MCLRE_OFF_3H;
/* Low voltage programming disabled, Stack Overflow Reset enabled */
code char at __CONFIG4L _conf4 = _LVP_OFF_4L & _STVREN_ON_4L;
/* Code protection disabled */
code char at __CONFIG5L _conf5 = _CP0_OFF_5L & _CP1_OFF_5L & _CP2_OFF_5L & 
_CP3_OFF_5L;
/* EEPROM read protection disabled, code protection boot disabled */
code char at __CONFIG5H _conf6 = _CPD_OFF_5H & _CPB_OFF_5H;
/* Table write protection disabled */
code char at __CONFIG6L _conf7 = _WRT0_OFF_6L & _WRT1_OFF_6L & _WRT2_OFF_6L & 
_WRT3_OFF_6L;
/* EEPROM write protection disabled, table write protect boot disabled, config 
write protect disabled */
code char at __CONFIG6H _conf8 = _WRTD_OFF_6H & _WRTB_OFF_6H & _WRTC_OFF_6H;
/* Table read protect disabled */
code char at __CONFIG7L _conf9 = _EBTR0_OFF_7L & _EBTR1_OFF_7L & _EBTR2_OFF_7L 
& _EBTR3_OFF_7L;
/* Table read protect boot disabled */
code char at __CONFIG7H _conf10 = _EBTRB_OFF_7H;

void serial_init()
{
    // BAUDCON - baudrate generation control Register
    BAUDCON = 0;  // 8 bit baudrate generator, auto-baud disabled

    // SPBRG - Baud Rate Generator Register
    SPBRG = BAUDRATE;

    // BRGH - High Baud Rate Select Bit
    TXSTAbits.BRGH = 1; // (1 = high speed)

    // SYNC - USART Mode select Bit
    TXSTAbits.SYNC = 0; // (0 = asynchronous)

    // TRISC - Tri-state Data Direction Register for port C
    // RC6 - 6th pin of port C - used for Serial Transmit
    // RC7 - 7th pin of port C - used for Serial Receive
    TRISCbits.TRISC6 = 0; // (0 = pin set as output)
    TRISCbits.TRISC7 = 1; // (1 = pin set as input)

    // SPEN - Serial Port Enable Bit
    RCSTAbits.SPEN = 1; // (1 = serial port enabled)

    // TXIE - USART Transmit Interupt Enable Bit
    PIE1bits.TXIE = 0; // (0 = disabled)

    // RCIE - USART Receive Interupt Enable Bit
    PIE1bits.RCIE = 1; // (1 = enabled)

    // TX9 - 9-bit Transmit Enable Bit
    TXSTAbits.TX9 = 0; // (0 = 8-bit transmit)

    // RX9 - 9-bit Receive Enable Bit
    RCSTAbits.RX9 = 0; // (0 = 8-bit reception)

    // CREN - Continuous Receive Enable Bit
    RCSTAbits.CREN = 1; // (1 = Enables receiver)

    // TXEN - Trasmit Enable Bit
    TXSTAbits.TXEN = 1; // (1 = transmit enabled)

    stdout = STREAM_USER;
}

void init()
{
    /* Internal oscillator @ 8MHz */
    OSCCON &= 0x8F;
    OSCCON |= 0x70;

    /* Disable watchdog */
    WDTCONbits.SWDTEN = 0;
}

void test_puts(data char *str)
{
    usart_putc(str[0]);
    while( usart_busy() );
}

void main(void)
/*
  The main function:
  Initialises the system, and starts all interrupt based tasks.
*/
{
    char tekst[2];
    tekst[0]='?';
    tekst[1]=0;

    init();

    serial_init();

    while(1) {
        usart_putc('[');
        while( usart_busy() );

        /* (1): Send single character; works */
        usart_putc('?');
        while( usart_busy() );

        /* (2): Send characters from array; works */
        usart_putc(tekst[0]);
        while( usart_busy() );

        /* (3): Pass array to function, which sends them as (2) does; not 
working, sending garbage */
        test_puts(tekst);
        while( usart_busy() );

        /* (4): send string using the library-functions; not working, infinite 
loop */
        usart_puts("?");
        while( usart_busy() );

        usart_putc(']');
        while( usart_busy() );
    }
}


-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Sdcc-user mailing list
Sdcc-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sdcc-user

Reply via email to