0% found this document useful (0 votes)
27 views34 pages

VXL Code

The document describes code for an Arduino program that controls LEDs using UART commands and measures temperature using an ADC. It initializes ports and peripherals, defines interrupt handlers for the UART and ADC, and contains a main loop that runs continuously.

Uploaded by

thuongtran20199
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views34 pages

VXL Code

The document describes code for an Arduino program that controls LEDs using UART commands and measures temperature using an ADC. It initializes ports and peripherals, defines interrupt handlers for the UART and ADC, and contains a main loop that runs continuously.

Uploaded by

thuongtran20199
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 34

#include <mega16.

h>
#include <delay.h>
#define LED PORTC
#define SW1 PINA.5
#define SW2 PINA.6

// Declare your global variables here


int i;
// Cong tac 1 on: 1 LED sang chay qua lai.
if (SW1==1)
{LED=0x01; delay_ms(100);
for (i=0;i<7;i++)
{LED=LED<<1; delay_ms(100);
if (SW1==0)
{LED=0x00;
break;}
}
for (i=0;i<6;i++)
{LED=LED>>1;delay_ms(100);
if (SW1==0)
{LED=0x00;
break;}
}
}
// Công tac 2 on: 8 LED sang dan sau do tat dan.
if (SW2==1)
{LED=0x00; delay_ms(100);
for (i=0;i<8;i++)
{LED=(LED<<1)|1;delay_ms(100);
if (SW2==0)
{LED=0x00;
break;}
}
}
//
// ADC + LCD
ADC_out=read_adc(0);
dienap = ADC_out*5000/1023; //theo mV 5000 mV = 5V
nhietdo=dienap/10;

chuc=nhietdo/10;
dvi=nhietdo%10;

lcd_gotoxy(0,0);
lcd_puts("Nhiet do la: ");
lcd_gotoxy(0,1);
lcd_putchar(chuc+0x30); // 0x30 ma ASCII
lcd_putchar(dvi+0x30);
// VD:
// unsigned int nhietdo = read_adc(1)*500/1023;
// unsigned char chuc= (nhietdo%100)/10;
// unsigned char dvi= (nhietdo%10);
// lcd_gotoxy(0,0);
// lcd_puts ("nhiet do la: " );
// lcd_gotoxy(0,1);
// lcd_putchar(chuc+0x30);
// lcd_putchar(dvi+0x30);

// ADC + LCD
// ADC_out=read_adc(0);
// dienap = ADC_out*5000/1023; //theo mV 5000 mV = 5V
// nhietdo=dienap/10;
//
// chuc=nhietdo/10;
// dvi=nhietdo%10;
//
// lcd_gotoxy(0,0);
// lcd_puts("Nhiet do la: ");
// lcd_gotoxy(0,1);
// lcd_putchar(chuc+0x30); // 0x30 ma ASCII
// lcd_putchar(dvi+0x30);
// VD:
// unsigned int nhietdo = read_adc(1)*500/1023;
// unsigned char chuc= (nhietdo%100)/10;
// unsigned char dvi= (nhietdo%10);
// lcd_gotoxy(0,0);
// lcd_puts ("nhiet do la: " );
// lcd_gotoxy(0,1);
// lcd_putchar(chuc+0x30);
// lcd_putchar(dvi+0x30);

//
/*****************************************************
This program was produced by the
CodeWizardAVR V2.05.0 Professional
Automatic Program Generator
© Copyright 1998-2010 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com

Project : CUOI KI VI XU LY
Version :
Date : 18/12/2023
Author : TRAN MINH THUONG
Company :
Comments:

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 <mega16.h>
#include <string.h>
#include <delay.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>

#ifndef RXB8
#define RXB8 1
#endif

#ifndef TXB8
#define TXB8 0
#endif

#ifndef UPE
#define UPE 2
#endif
#ifndef DOR
#define DOR 3
#endif

#ifndef FE
#define FE 4
#endif

#ifndef UDRE
#define UDRE 5
#endif

#ifndef RXC
#define RXC 7
#endif

#define FRAMING_ERROR (1<<FE)


#define PARITY_ERROR (1<<UPE)
#define DATA_OVERRUN (1<<DOR)
#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC)

#define LED PORTB

// 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

// khai bao bien buffer va index


#define RX_BUFFER_SIZE 8
char rx_buffer[RX_BUFFER_SIZE];
unsigned char rx_wr_index = 0;
unsigned char rx_rd_index = 0;
unsigned char rx_counter = 0;

bit rx_buffer_overflow;

// ham ngat USART_RXC de su dung BUFFER

// chuong trinh con phat 1 ky tu


void uart_char_send(unsigned char chr) {
// kiem tra khi nao bit UDRE = 1
while (!(UCSRA & (1<<UDRE))) {};
UDR = chr; // bo du lieu vao thanh ghi UDR
}
// chuong trinh con phat 1 chuoi ky tu
void uart_string_send(unsigned char *txt) {
unsigned char n,i;
n=strlen(txt); // dem so ky tu
for(i=0;i<n;i++)
{
uart_char_send(txt[i]);
}
}
// ngat nhan UART
interrupt [USART_RXC] void usart_rx_isr(void)
{
char status, data;
status = UCSRA;
data = UDR;

switch (data)
{
case '1':
PORTB.0 = 1; // Turn on the LED
break;
case '2':
PORTB.1 = 1; // Turn on the LED
break;
case '3':
PORTB.2 = 1; // Turn on the LED
break;
case '4':
PORTB.3 = 1; // Turn on the LED
break;
case '5':
PORTB.4 = 1; // Turn on the LED
break;
case '6':
PORTB.5 = 1; // Turn on the LED
break;
case '7':
PORTB.6 = 1; // Turn on the LED
break;
case '8':
PORTB.7 = 1; // Turn on the LED
break;
// tat den
case 'a':
PORTB.0 = 0; // Turn off the LED
break;
case 'b':
PORTB.1 = 0; // Turn off the LED
break;
case 'c':
PORTB.2 = 0; // Turn off the LED
break;
case 'd':
PORTB.3 = 0; // Turn off the LED
break;
case 'e':
PORTB.4 = 0; // Turn off the LED
break;
case 'f':
PORTB.5 = 0; // Turn off the LED
break;
case 'g':
PORTB.6 = 0; // Turn off the LED
break;
case 'h':
PORTB.7 = 0; // Turn off the LED
break;
// Add more cases if needed

default:
// Handle other characters if needed
break;
}
rx_buffer[rx_wr_index++] = data;
#if RX_BUFFER_SIZE == 256
// special case for receiver buffer size=256
if (++rx_counter == 0)
{
#else
if (rx_wr_index == RX_BUFFER_SIZE) rx_wr_index=0;
if (++rx_counter == RX_BUFFER_SIZE)
{
rx_counter=0;
#endif
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_BUFFER_SIZE != 256
if (rx_rd_index == RX_BUFFER_SIZE) rx_rd_index=0;
#endif
#asm("cli")
--rx_counter;
#asm("sei")
return data;
}
#pragma used-
#endif

#include <stdio.h>

#define ADC_VREF_TYPE 0x00

// doc ADC
unsigned int read_adc(unsigned char adc_input)
{
ADMUX=adc_input | (ADC_VREF_TYPE & 0xff);
delay_us(10);
ADCSRA|=0x40;
while ((ADCSRA & 0x10)==0);
ADCSRA|=0x10;
return ADCW;
}
unsigned int i=0;
unsigned long dienap;
unsigned long ADC_out=0;
unsigned long nhietdo;
unsigned char chuc, dvi;

// ham xulylenh tu buffer


//void processCommand(char *buffer, unsigned char length)
//{
// if (length > 0)
// {
// switch (buffer[0])
// {
// case '1':
// PORTB.0 = 1;
// break;
// case '2':
// PORTB.1 = 1;
// break;
// case '3':
// PORTB.2 = 1;
// break;
// case '4':
// PORTB.3 = 1;
// break;
// case '5':
// PORTB.4 = 1;
// break;
// case '6':
// PORTB.5 = 1;
// break;
// case '7':
// PORTB.6 = 1;
// break;
// case '8':
// PORTB.7 = 1;
// break;
// case 'a':
// PORTB.0 = 0;
// break;
// case 'b':
// PORTB.1 = 0;
// break;
// case 'c':
// PORTB.2 = 0;
// break;
// case 'd':
// PORTB.3 = 0;
// break;
// case 'e':
// PORTB.4 = 0;
// break;
// case 'f':
// PORTB.5 = 0;
// break;
// case 'g':
// PORTB.6 = 0;
// break;
// case 'h':
// PORTB.7 = 0;
// break;
// default:
// // Handle unsupported commands or do nothing
// break;
// }
// }
//}

void main(void)
{
bit ledState = 0;
// 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;
// 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=0xff;
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In
Func0=Out
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=0
PORTC=0x00;
DDRC=0x01;
// 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=0x02;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=0xFF
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer1 Stopped
// Mode: Normal top=0xFFFF
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 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: Timer2 Stopped
// Mode: Normal top=0xFF
// 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=0x98;
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: ADC Stopped
ADMUX=ADC_VREF_TYPE & 0xff;
ADCSRA=0x83;

// SPI initialization
// SPI disabled
SPCR=0x00;

// TWI initialization
// TWI disabled
TWCR=0x00;

// cho phep ngat toan cuc


#asm("sei")
while (1)
{
//LED
PORTC.0 = ledState;
// LED=0x00; delay_ms(100);
// for (i=0;i<8;i++)
// {
// LED=(LED<<1)|1;
// delay_ms(100);
// }
// buffer
// if (rx_counter > 0)
// {
// #asm("cli")
// processCommand(rx_buffer, rx_counter);
//
// rx_wr_index = 0;
// rx_rd_index = 0;
// rx_counter = 0;
// rx_buffer_overflow = 0;
//
// #asm("sei")
// }
// nhiet do
ADC_out=read_adc(0);
dienap = ADC_out*5000/1023; //theo mV 5000 mV = 5V
nhietdo=dienap/10;

chuc=nhietdo/10;
dvi=nhietdo%10;

uart_string_send("Nhiet do la: ");


uart_char_send(chuc+0x30);
uart_char_send(dvi+0x30);
uart_char_send(10); // ket thuc chuoi
uart_char_send(13); // bang ma ASCII - xuong hang
delay_ms(1000);
ledState = !ledState;
}
}

//

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Rebar;
using System.Reflection;
using System.Security.AccessControl;

namespace giaodien
{
public partial class Code : Form
{
private int counter = 0;
private bool isConnected = false;
public string temperature;
public double time_temperature;

public double d_toado_x = 0;

public Code()
{
InitializeComponent();

}
string[] pause = { "1200", "2400", "4800", "9600", "19200", "38400",
"57600", "14880" };
private void Code_Load(object sender, EventArgs e)
{
string[] listnamecom = SerialPort.GetPortNames();
congcom.Items.AddRange(listnamecom);
baudrate.Items.AddRange(pause);
UpdateControls();
}

private void ketnoi_Click(object sender, EventArgs e)


{
try
{
if (congcom.Text == "" || baudrate.Text == "")
{
MessageBox.Show("Bạn chưa nhập đủ thông tin", "Thông báo");
}
if (serialPort1.IsOpen)
{
serialPort1.Close();
isConnected = false;
ketnoi.Text = "CONNECT";
timer_chart.Enabled = time_nhietdo.Enabled = false;
}
else
{
serialPort1.PortName = congcom.Text;
serialPort1.BaudRate = int.Parse(baudrate.Text);
serialPort1.Open();
isConnected = true;
ketnoi.Text = "DISCONNECT";
timer_chart.Enabled = time_nhietdo.Enabled = true;
}
UpdateControls();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}

private void LED_1_Click(object sender, EventArgs e)


{
if (LED_1.Text == "BẬT ĐÈN 1")
{
serialPort1.Write("1");
LED1.Image = global::giaodien.Properties.Resources.batden;
LED_1.Text = "TẮT ĐÈN";
}
else if (LED_1.Text == "TẮT ĐÈN")
{
serialPort1.Write("a");
LED1.Image = global::giaodien.Properties.Resources.bongden;
LED_1.Text = "BẬT ĐÈN 1";
}
}

private void LED_2_Click(object sender, EventArgs e)


{
if (LED_2.Text == "BẬT ĐÈN 2")
{
serialPort1.Write("2");
LED2.Image = global::giaodien.Properties.Resources.batden;
LED_2.Text = "TẮT ĐÈN";
}
else if (LED_2.Text == "TẮT ĐÈN")
{
serialPort1.Write("b");
LED2.Image = global::giaodien.Properties.Resources.bongden;
LED_2.Text = "BẬT ĐÈN 2";
}
}

private void LED_3_Click(object sender, EventArgs e)


{
if (LED_3.Text == "BẬT ĐÈN 3")
{
serialPort1.Write("3");
LED3.Image = global::giaodien.Properties.Resources.batden;
LED_3.Text = "TẮT ĐÈN";
}
else if (LED_3.Text == "TẮT ĐÈN")
{
serialPort1.Write("c");
LED3.Image = global::giaodien.Properties.Resources.bongden;
LED_3.Text = "BẬT ĐÈN 3";
}
}

private void LED_4_Click(object sender, EventArgs e)


{
if (LED_4.Text == "BẬT ĐÈN 4")
{
serialPort1.Write("4");
LED4.Image = global::giaodien.Properties.Resources.batden;
LED_4.Text = "TẮT ĐÈN";
}
else if (LED_4.Text == "TẮT ĐÈN")
{
serialPort1.Write("d");
LED4.Image = global::giaodien.Properties.Resources.bongden;
LED_4.Text = "BẬT ĐÈN 4";
}
}

private void LED_5_Click(object sender, EventArgs e)


{
if (LED_5.Text == "BẬT ĐÈN 5")
{
serialPort1.Write("5");
LED5.Image = global::giaodien.Properties.Resources.batden;
LED_5.Text = "TẮT ĐÈN";
}
else if (LED_5.Text == "TẮT ĐÈN")
{
serialPort1.Write("e");
LED5.Image = global::giaodien.Properties.Resources.bongden;
LED_5.Text = "BẬT ĐÈN 5";
}
}

private void LED_6_Click(object sender, EventArgs e)


{
if (LED_6.Text == "BẬT ĐÈN 6")
{
serialPort1.Write("6");
LED6.Image = global::giaodien.Properties.Resources.batden;
LED_6.Text = "TẮT ĐÈN";
}
else if (LED_6.Text == "TẮT ĐÈN")
{
serialPort1.Write("f");
LED6.Image = global::giaodien.Properties.Resources.bongden;
LED_6.Text = "BẬT ĐÈN 6";
}
}

private void LED_7_Click(object sender, EventArgs e)


{
if (LED_7.Text == "BẬT ĐÈN 7")
{
serialPort1.Write("7");
LED7.Image = global::giaodien.Properties.Resources.batden;
LED_7.Text = "TẮT ĐÈN";
}
else if (LED_7.Text == "TẮT ĐÈN")
{
serialPort1.Write("g");
LED7.Image = global::giaodien.Properties.Resources.bongden;
LED_7.Text = "BẬT ĐÈN 7";
}
}

private void LED_8_Click(object sender, EventArgs e)


{
if (LED_8.Text == "BẬT ĐÈN 8")
{
serialPort1.Write("8");
LED8.Image = global::giaodien.Properties.Resources.batden;
LED_8.Text = "TẮT ĐÈN";
}
else if (LED_8.Text == "TẮT ĐÈN")
{
serialPort1.Write("h");
LED8.Image = global::giaodien.Properties.Resources.bongden;
LED_8.Text = "BẬT ĐÈN 8";
}
}

private void dem_Click(object sender, EventArgs e)


{
counter++;
hienthicounter.Text = counter.ToString();

}
private void reset_Click(object sender, EventArgs e)
{
counter = 0;
hienthicounter.Text = counter.ToString();

}
private void UpdateControls()
{
ketnoi.Text = isConnected ? "DISCONNECT" : "CONNECT";
// ĐÈN
LED_1.Enabled = isConnected;
LED_2.Enabled = isConnected;
LED_3.Enabled = isConnected;
LED_4.Enabled = isConnected;
LED_5.Enabled = isConnected;
LED_6.Enabled = isConnected;
LED_7.Enabled = isConnected;
LED_8.Enabled = isConnected;
// counter
dem.Enabled = isConnected;
reset.Enabled = isConnected;
// chỉnh màu
if (isConnected)
{
// chua connect
LED_1.BackColor = Color.Black;
LED_2.BackColor = Color.Black;
LED_3.BackColor = Color.Black;
LED_4.BackColor = Color.Black;
LED_5.BackColor = Color.Black;
LED_6.BackColor = Color.Black;
LED_7.BackColor = Color.Black;
LED_8.BackColor = Color.Black;
}
else
{
// khi connect
LED_1.BackColor = SystemColors.Control;
LED_2.BackColor = SystemColors.Control;
LED_3.BackColor = SystemColors.Control;
LED_4.BackColor = SystemColors.Control;
LED_5.BackColor = SystemColors.Control;
LED_6.BackColor = SystemColors.Control;
LED_7.BackColor = SystemColors.Control;
LED_8.BackColor = SystemColors.Control;
}
}
private void serialPort1_DataReceived(object sender,
SerialDataReceivedEventArgs e)
{
temperature = serialPort1.ReadLine();
try
{
BeginInvoke(new Action(() =>
{
nhietdo.Text = temperature;
}));
}
catch (Exception)
{
MessageBox.Show("ERROR");
}

private void start_chart_Click(object sender, EventArgs e)


{
this.chart1.Series["Nhiệt độ"].Points.AddXY(0, 5);
this.chart1.Series["Nhiệt độ"].Points.AddXY(1, 10);
this.chart1.Series["Nhiệt độ"].Points.AddXY(2, 15);
this.chart1.Series["Nhiệt độ"].Points.AddXY(3, 20);
this.chart1.Series["Nhiệt độ"].Points.AddXY(4, 25);
}

private void time_nhietdo_Tick(object sender, EventArgs e)


{
if (serialPort1.IsOpen)
{
try
{
if (temperature != null)
{
time_temperature = Double.Parse(temperature);
}
}
catch (Exception)
{
serialPort1.Close();
temperature = null;
MessageBox.Show("Dữ liệu lỗi, Vui lòng kiểm tra lại");
}
}
else
{
time_nhietdo.Enabled = false;
}

private void timer_chart_Tick(object sender, EventArgs e)


{
// kỹ thuật update đầu
if (serialPort1.IsOpen)
{
this.chart1.Series["Nhiệt độ"].Points.AddXY(d_toado_x,
time_temperature);
d_toado_x++;
// kỹ thuật xóa bộ nhớ đệm để update biểu đồ
if (d_toado_x > 20)
{
chart1.Series["Nhiệt độ"].Points.Clear();
d_toado_x = 0;
}
}
else
{
timer_chart.Enabled = false;
MessageBox.Show("Chưa kết nối, Vui lòng kiểm tra connect");
}

}
}
}

You might also like