Saturday, 22 June 2013

Generic Software Debug UART implementation for Microcontrollers.



         In this post I am giving code snippet of software debug uart implementation which can be used for any microcontroller. I did not implemented receive functionality as for debug uart it is rarely required. I will post it soon. Now understand the code details in short. What i did is you need to have start bit, a stop bit and data bits. Start bit is High to low transition for one bit period (1/baud rate) and stop bit is low to high transition for 1 bit period. and you need to shift data bits (LSB first) of a character byte in these two bits over a GPIO. For Tx configure port as Output. You can use almost any port pin for this.I have written and tested below code on atmega8 with maximum 9600 baud rate. You can use this uart for debugging purpose if your code is complicated and lengthy.


softuart.h
------------------------------------------------------------------------------------------------------------
/*
 * File :softuart.h
 * Created on: 21-Jun-2013
 * Author: Shrenik S. Shikhare
 * Copyright(c): 2013
 */


#define START_BIT 1
#define DATA_BITS 8
#define NUMBER_OF_STOP_BIT 1

#define F_CPU 1000000
#define TRANSMIT_PIN PD1
#define RECEIVE_PIN PD0
#define DEBUG_BAUD_RATE 9600 //work only upto 9600
#define DELAY_VALUE (F_CPU /DEBUG_BAUD_RATE)

void InitDebugUart();
void putDebugChar(char data);
void DEBUG(char *data);
------------------------------------------------------------------------------------------------------------





softuart.c
------------------------------------------------------------------------------------------------------------
 * File :softuart.c
 * Created on: 21-Jun-2013
 * Author: Shrenik S. Shikhare
 * Copyright(c): 2013
 */


#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/delay.h>
#include "softuart.h"


/****************************************************************************************
 * Function name : InitDebugUart()
 *Discription : This configures uart related pins like TxPIN as output and RxPIN as input
****************************************************************************************/
void InitDebugUart()
{
DDRD &= ~(1<<RECEIVE_PIN) ; //PD0 is RXD
DDRD |= (1<<TRANSMIT_PIN)|(1<<PD2); //PD1 is TXD
PORTD |= (1<<TRANSMIT_PIN); //Make Trasmit pin default high at start
}


/****************************************************************************************
 * Function name : putDebugChar()
 *Discription : This function toggles Tx GPIO as per data input with delay between two bit = bit period
   for the required baud rate bit period=1/BAUD_RATE
****************************************************************************************/

void putDebugChar(char data)
{
uint8_t Iterator;
PORTD &= (0<<TRANSMIT_PIN); //Make trasmit pin high to low to send start bit
//_delay_us(104);
_delay_us(DELAY_VALUE);
for (Iterator=0; Iterator<8;Iterator++)
{
if (data & 0x01)
PORTD |= (1<<TRANSMIT_PIN);
else
PORTD &= (0<<TRANSMIT_PIN);
_delay_us(DELAY_VALUE-4);//this is done to compensate for m/c cycles that are used in further instruction execution
data = data >> 1; //approx delay for 9600Baud
//data = data >> 1;
}

PORTD |= (1<<TRANSMIT_PIN); //Send logic 1 stop bit for a bit period
_delay_us(104);

}

void DEBUG(char *data)
{
while(*data)
putDebugChar(*data++);

}

------------------------------------------------------------------------------------------------------------

You can change this code as per the MCU you are going to use. You wont required to change much apart from delay routine to get required bit period, controller specific header files.
I will update receive uart code soon. Hope this will be useful for you.

Thanks 
Shrenik.


Find me on LinkedIn, Facebook.   

Saturday, 16 February 2013

Zigbee irrigation system


 

This is a simple approach to see how to implement zigbee based water irrigation control for irrigation system. This is not a difficult thing to get control over motor and electromagnetic valves using zigbee and MCU. I am not going to provide detailed steps how its implemented as code provided is with lot of comments.

The idea is we can control the electromagnetic valves and motor depending on the moister content in the farm area and the water level in the water source (water well).

Its somewhat advanced technique which requires PC/laptop for manual control. This system is having both manual and automatic control. For manual control you can control valves and motor from remote place, and for automatic control the simple moister sensor like this can be used. That moister sensor will give digital logic output based on whether soil is wet or dry. If its dry and the water level is good then motor and respective valve will get turned on.

Now to setup communication between two zigbee modules refer to Xbee setup.

You can download the xbeeirri.c file and the proteus simulation file (not complete simulation, leds are used instead of valves and switches are used as moister sensor to test the simulation) from my repository . I have implemented actual hardware also but its not with me right now. i will post pics later on.

This is very short post for the project as I am not getting much time to post in detail on blogs ;).

Your suggestions are appreciable. Sorry for incontinence I am not able to post complete HW details for this.




Regards,
Shrenik.


Have a look at my interview on EEWeb community. click here

click here to see this online simulator- http://www.partsim.com/




`