-- Copyright (C) 2002 Javier Martínez -- -- This software is provided 'as-is', without any express or implied -- warranty. In no event will the authors be held liable for any damages -- arising from the use of this software. -- -- Permission is granted to anyone to use this software for any purpose, -- including commercial applications, and to alter it and redistribute it -- freely, subject to the following restrictions: -- -- 1. The origin of this software must not be misrepresented; -- you must not claim that you wrote the original software. -- If you use this software in a product, an acknowledgment in -- the product documentation would be appreciated but is not required. -- -- 2. Altered source versions must be plainly marked as such, and must -- not be misrepresented as being the original software. -- -- 3. This notice may not be removed or altered from any source distribution. -- -- This license is commonly known as the zlib/libpng License. -- -- -- -------------------------------------------------------------------- -- file : f452_serial.jal -- author : Javier Martínez -- date : 20021113 -- purpose : 18F452 Hardware serial module -- requires : jpic16,jal -- -- Copyright (C) 2002 Javier Martínez -- -- See readme.txt -- -------------------------------------------------------------------- -- Declarations -- -------------------------------------------------------------------- include jpic16 -- -------------------------------------------------------------------- -- USER configuration area -- -------------------------------------------------------------------- const BAUD_CONSTANT = 9600 -- const BAUD_CONSTANT = 57600 -- Select a baudrate from this table: -- ================================= -- Fosc = 4MHz -- _________________________________ -- Baud %Err SPBRG BRGH -- -- 300 -0.17 0xCF 0 -- 1200 0.17 0xCF 1 -- 9600 0.16 0x19 1 -- 19200 0.16 0x0C 1 -- ================================= -- Fosc = 10MHz -- _________________________________ -- Baud %Err SPBRG BRGH -- -- 1200 0.16 0x81 0 -- 9600 0.16 0x40 1 -- ================================= -- Fosc = 20MHz -- _________________________________ -- Baud %Err SPBRG BRGH -- -- 9600 0.16 0x81 1 -- 19200 0.16 0x40 1 -- ================================= -- Fosc = 40MHz -- _________________________________ -- Baud %Err SPBRG BRGH -- -- 9600 -0.16 0x40 0 -- 19200 -0.16 0x81 1 -- 38400 -0.16 0x40 1 -- 57600 -0.93 0x2A 1 -- -------------------------------------------------------------------- -- Body -- -------------------------------------------------------------------- procedure f452_serial_setup is begin pin_c7_direction = input pin_c6_direction = output TXSTA = 0x00 if ( ( target_clock == 4_000_000 ) & ( BAUD_CONSTANT == 300 ) ) | ( ( target_clock == 10_000_000 ) & ( BAUD_CONSTANT == 1_200 ) ) | ( ( target_clock == 40_000_000 ) & ( BAUD_CONSTANT == 9_600 ) ) then SPBRG = ( target_clock / ( 64 * BAUD_CONSTANT ) ) - 1 TXSTA_BRGH = low else SPBRG = ( target_clock / ( 16 * BAUD_CONSTANT ) ) - 1 TXSTA_BRGH = high end if RCSTA = 0x00 TXSTA_TXEN = High RCSTA_CREN = high RCSTA_SPEN = high asm movf RCREG , w -- clear init buffer asm movf RCREG , w end procedure function getc'get return byte is while ! PIR1_RCIF loop end loop return RCREG end function procedure putc'put ( byte in data ) is begin while ! PIR1_TXIF loop end loop TXREG = data end procedure