USART in ATMEGA16, with VB.NET code. (Serial Port Communication)
This code is reading Voltage from ADC1 and send it on Serial port and reading the incoming serial data.
On the other hand VB.NET code will receive Serial data (Voltage) and sends a string serially
/*****************************************************
Chip type : ATmega16
Program type : Application
AVR Core Clock frequency: 8.000000 MHz
Memory model : Small
External RAM size : 0
Data Stack size : 256
*****************************************************/
#include
#include
// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0x15 ;PORTC
#endasm
#include
// Standard Input/Output functions
#include
#include
#define RXB8 1
#define TXB8 0
#define UPE 2
#define OVR 3
#define FE 4
#define UDRE 5
#define RXC 7
#define FRAMING_ERROR (1<
#define PARITY_ERROR (1<
#define DATA_OVERRUN (1<
#define DATA_REGISTER_EMPTY (1<
#define RX_COMPLETE (1<
// USART Receiver buffer
#define RX_BUFFER_SIZE 8
char rx_buffer[RX_BUFFER_SIZE];
#if RX_BUFFER_SIZE<256
unsigned char rx_wr_index,rx_rd_index,rx_counter;
#else
unsigned int rx_wr_index,rx_rd_index,rx_counter;
#endif
// This flag is set on USART Receiver buffer overflow
bit rx_buffer_overflow;
unsigned char REC_DATA[20];
int J=0;
// USART Receiver interrupt service routine
interrupt [USART_RXC] void usart_rx_isr(void)
{
char status,data;
status=UCSRA;
data=UDR;
if (data == '?')
{
J=0;
//sprintf(REC_DATA," ") ;
}
else
{
REC_DATA[J]=data;
J++;
}
if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
{
rx_buffer[rx_wr_index]=data;
if (++rx_wr_index == RX_BUFFER_SIZE) rx_wr_index=0;
if (++rx_counter == RX_BUFFER_SIZE)
{
rx_counter=0;
rx_buffer_overflow=1;
};
};
}
#ifndef _DEBUG_TERMINAL_IO_
// Get a character from the USART Receiver buffer
#define _ALTERNATE_GETCHAR_
#pragma used+
char getchar(void)
{
char data;
//while (rx_counter==0);
data=rx_buffer[rx_rd_index];
if (++rx_rd_index == RX_BUFFER_SIZE) rx_rd_index=0;
#asm("cli")
--rx_counter;
#asm("sei")
return data;
}
#pragma used-
#endif
// USART Transmitter buffer
#define TX_BUFFER_SIZE 8
char tx_buffer[TX_BUFFER_SIZE];
#if TX_BUFFER_SIZE<256
unsigned char tx_wr_index,tx_rd_index,tx_counter;
#else
unsigned int tx_wr_index,tx_rd_index,tx_counter;
#endif
// USART Transmitter interrupt service routine
interrupt [USART_TXC] void usart_tx_isr(void)
{
if (tx_counter)
{
--tx_counter;
UDR=tx_buffer[tx_rd_index];
if (++tx_rd_index == TX_BUFFER_SIZE) tx_rd_index=0;
};
}
#ifndef _DEBUG_TERMINAL_IO_
// Write a character to the USART Transmitter buffer
#define _ALTERNATE_PUTCHAR_
#pragma used+
void putchar(char c)
{
while (tx_counter == TX_BUFFER_SIZE);
#asm("cli")
if (tx_counter || ((UCSRA & DATA_REGISTER_EMPTY)==0))
{
tx_buffer[tx_wr_index]=c;
if (++tx_wr_index == TX_BUFFER_SIZE) tx_wr_index=0;
++tx_counter;
}
else
UDR=c;
#asm("sei")
}
#pragma used-
#endif
#define ADC_VREF_TYPE 0x00
// Read the AD conversion result
unsigned int read_adc(unsigned char adc_input)
{
ADMUX=adc_input | (ADC_VREF_TYPE & 0xff);
// Delay needed for the stabilization of the ADC input voltage
delay_us(10);
// Start the AD conversion
ADCSRA|=0x40;
// Wait for the AD conversion to complete
while ((ADCSRA & 0x10)==0);
ADCSRA|=0x10;
return ADCW;
}
// Declare your global variables here
void main(void)
{
unsigned int volt;
unsigned char b[20];
int i=0;
float f=0;
// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTA=0x00;
DDRA=0x00;
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0x00;
DDRB=0x00;
// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;
// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;
// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 9600
UCSRA=0x00;
UCSRB=0xD8;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x33;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
// ADC initialization
// ADC Clock frequency: 1000.000 kHz
// ADC Voltage Reference: AREF pin
// ADC Auto Trigger Source: None
ADMUX=ADC_VREF_TYPE & 0xff;
ADCSRA=0x83;
// LCD module initialization
lcd_init(20);
// Global enable interrupts
#asm("sei")
while (1)
{
ADMUX=0x21 & 0xff ;
lcd_clear();
lcd_gotoxy(0,0);
delay_ms(10);
lcd_putsf("Volt= ");
volt=(unsigned int) read_adc(1);
f=volt;
f= 5 * (f/1023);
ftoa(f,2,b); // till 4 decimal places
sprintf(b,"%s",b) ;
lcd_puts(b);
lcd_gotoxy(13,0);
delay_ms(10);
lcd_putsf("V");
lcd_gotoxy(0,1);
sprintf(b," ") ;
sprintf(b,"%u?",volt) ;
lcd_puts(b);
for (i=0;i<20;i++)
{
putchar(b[i]);
if (b[i] =='?')
break;
}
sprintf(b," ") ;
/*
for (i=0;i<20;i++)
{
b[i]=getchar();
if (b[i] =='?' || b[i] =='' || b[i] ==' ')
break;
delay_ms(50);
}
lcd_gotoxy(0,2);
lcd_puts(b);
sprintf(b," ") ;
*/
lcd_gotoxy(0,2);
lcd_puts(REC_DATA);
sprintf(REC_DATA," ") ;
lcd_gotoxy(5,3);
lcd_putsf("SER COMM");
delay_ms(1000);
};
}
VB.NET code
1) Open serial Port
If Not SerialPort1.IsOpen Then
SerialPort1.PortName = "Com3" 'port
' Set other port parameters.
SerialPort1.BaudRate = 9600
SerialPort1.Parity = Parity.None
SerialPort1.DataBits = 8
SerialPort1.StopBits = StopBits.One
SerialPort1.Handshake = Handshake.None
SerialPort1.WriteBufferSize = 100000
SerialPort1.DtrEnable = False
SerialPort1.RtsEnable = False
SerialPort1.Open()
End If
2) On Data_received event
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
System.Threading.Thread.Sleep(100)
'DATA_RECEIVED = DATA_RECEIVED & SerialPort1.ReadExisting()
'DATA_RECEIVED_FLAG = 1
Dim bRead, nRead As Integer
Dim returnStr As String = ""
Dim ascStr As String = ""
bRead = SerialPort1.BytesToRead 'Number of Bytes to read
Dim cData(bRead - 1) As Byte
nRead = SerialPort1.Read(cData, 0, bRead) 'Reading the Data
For Each b As Byte In cData
DATA_RECEIVED_1 = DATA_RECEIVED_1 & " " & Chr(b)
Next
DATA_RECEIVED_FLAG_1 = 1
End Sub
''There are many ways, one can choose any method.
3) Display data received and send data (On serial) (Using timer)
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If DATA_RECEIVED_FLAG_1 = 1 Then
TextBox3.Text = DATA_RECEIVED_1 & Environment.NewLine
DATA_RECEIVED_1 = ""
DATA_RECEIVED_FLAG_1 = 0
End If
If Me.CheckBox1.Checked Then
SerialPort1.Write("VIKAS") 'String will be displayed on LCD
System.Threading.Thread.Sleep(20)
End If
End Sub
''If CheckBox1 is checked then send data otherwise not.
No comments:
Post a Comment