FRDM-KE04Z Software bit banging

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

FRDM-KE04Z Software bit banging

1,078件の閲覧回数
vikranthnk
Contributor I

I am using FRDM-KE04Z dev board with onboard MKE04Z8VFK4 MCU, I an=m trying to implement bit banging on this board using 2 GPIO pins one for RX and one for TX. During implementation am unuseful with FRDM-KE04Z. Someone can help me with implementing software UART on this board. As am new to this platform and am unable to configure properly. 

Note: The below code has been tested and working on microchip boards. I am to port the same code to FRDM-KE04Z dev board.

My code.

#include "common.h"
#include "ics.h"
#include "gpio.h"
#include "sysinit.h"
#include "start.h"


int main (void);


* Global functions
******************************************************************************/
unsigned char UART_Receive(void);
void UART_Transmit(const char DataValue);
void DelayUs(unsigned int cnt);
#define UART_RX    GPIO_PTA3 // UART RX pin
#define UART_TX     GPIO_PTA2 // UART TX pin
#define DataBitCount   8 // no parity, no flow control
#define OneBitDelay    2 //(1000000/1200) // microseconds
//#define GPIOA ((GPIO_Type *)GPIOA_BASE)

/********************************************************************/
int main (void)
{
/* Perform processor initialization */
sysinit();
cpu_identify();

/* way 2. GPIO initialize single pin name */

GPIO_PinInit(GPIO_PTA2, GPIO_PinOutput);
GPIO_PinInit(GPIO_PTA3, GPIO_PinInput);

unsigned char ch = 0; // Variable to store Rx character
GPIO_PinSet(UART_TX);  / /TX pin is high in idle state
while (1)
{
ch = UART_Receive(); // Receive a character from UART
UART_Transmit(ch); // Echo back that character
}

}

unsigned char UART_Receive(void)
{
unsigned char DataValue = 0;
uint32_t data;

//wait for start bit
while(UART_RX==1);

DelayUs(OneBitDelay);
DelayUs(OneBitDelay/2); // Take sample value in the mid of bit duration

for ( unsigned char i = 0; i < DataBitCount; i++ )
{
if ( UART_RX == 1 ) //if received bit is high
{
DataValue += (1<<i);
}

DelayUs(OneBitDelay);
}

// Check for stop bit
if ( UART_RX == 1 ) //Stop bit should be high
{
DelayUs(OneBitDelay/2);
return DataValue;
}
else //some error occurred !
{
DelayUs(OneBitDelay/2);
return 0x000;
}
}

void UART_Transmit(const char DataValue)
{
/* Basic Logic

TX pin is usually high. A high to low bit is the starting bit and
a low to high bit is the ending bit. No parity bit. No flow control.
BitCount is the number of bits to transmit. Data is transmitted LSB first.

*/

// Send Start Bit
GPIO_PinClear(UART_TX);
DelayUs(OneBitDelay);

for ( unsigned char i = 0; i < DataBitCount; i++ )
{
//Set Data pin according to the DataValue
if( ((DataValue>>i)&0x1) == 0x1 ) //if Bit is high
{
GPIO_PinSet(UART_TX);
}
else //if Bit is low
{
GPIO_PinClear(UART_TX);
}

DelayUs(OneBitDelay);
}

//Send Stop Bit
GPIO_PinSet(UART_TX);
DelayUs(OneBitDelay);
}

void DelayUs(unsigned int cnt)
{
while (--cnt != 0)continue;
}

0 件の賞賛
返信
1 返信

906件の閲覧回数
mjbcswitzerland
Specialist V

Hi

Since the KE02 has 3 UARTs, I am assuming that this is a school exercise in bit-banging since it otherwise doesn't make much sense to do it like this.

However, if you need a fast solution just take the Open Source uTasker FRDM-KE02Z project at the links below and replace the following in your code:

1. Initialise the pins with
#define UART_RX   KE_PORTA_BIT3
#define UART_TX   KE_PORTA_BIT2
_CONFIG_PORT_INPUT(A, (UART_RX), PORT_PS_UP_ENABLE);
_CONFIG_DRIVE_PORT_OUTPUT_VALUE(A, (UART_TX), (UART_TX), (PORT_SRE_SLOW | PORT_DSE_HIGH)); // initialise driving '1'

2. For the delay use
fnDelayLoop(2);

3. To read the input pin state use
if (_READ_PORT_MASK(A, UART_RX) != 0) {
    // input is '1'
}
else {
    // input is '0'
}

4. To set the UART_TX low use
_CLEARBITS(A, UART_TX);
and to set it high use
_SETBITS(A, UART_TX);


You can find all port macros described at http://www.utasker.com/forum/index.php?topic=1875.0


Regards

Mark

Kinetis: http://www.utasker.com/kinetis.html
Kinetis KE:
- http://www.utasker.com/kinetis/FRDM-KE02Z.html
- http://www.utasker.com/kinetis/FRDM-KE02Z40M.html
- http://www.utasker.com/kinetis/FRDM-KE04Z.html
- http://www.utasker.com/kinetis/FRDM-KE06Z.html
- http://www.utasker.com/kinetis/FRDM-KE15Z.html
HW Timers: http://www.utasker.com/docs/uTasker/uTaskerHWTimers.PDF

Free Open Source solution: https://github.com/uTasker/uTasker-Kinetis
Working project in 15 minutes video: https://youtu.be/K8ScSgpgQ6M

For better, faster, cheaper product developments consider the uTasker developer's version, professional Kinetis support, one-on-one training and complete fast-track project solutions to set you apart from the herd : http://www.utasker.com/support.html

0 件の賞賛
返信