0% found this document useful (0 votes)
11 views31 pages

22H1120059 Lab5

The document contains code snippets for Arduino and C# applications that interact with hardware components. The Arduino code manages LED states based on serial input and reads data from sensors, while the C# code provides a Windows Forms interface for sending commands to the Arduino. The applications are designed to control and display data from connected devices using serial communication.

Uploaded by

Văn Ng
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)
11 views31 pages

22H1120059 Lab5

The document contains code snippets for Arduino and C# applications that interact with hardware components. The Arduino code manages LED states based on serial input and reads data from sensors, while the C# code provides a Windows Forms interface for sending commands to the Arduino. The applications are designed to control and display data from connected devices using serial communication.

Uploaded by

Văn Ng
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/ 31

Bài 1:

a)

b)char dulieu='\0';

#define led 9

void serialEvent() {

while (Serial.available()) {

// get the new byte:

char inChar = (char)Serial.read();

// add it to the inputString:

dulieu=inChar;

void setup() {

// put your setup code here, to run once:

Serial.begin(9600);
delay(100);

pinMode(led,OUTPUT);

void loop() {

// put your main code here, to run repeatedly:

switch(dulieu)

case '1':

digitalWrite(led,HIGH);

case '2':

digitalWrite(led,LOW);

default:

break;

using System;

using System.Collections.Generic;
using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

namespace WindowsFormsApplication1

public partial class Form1 : Form

public Form1()

InitializeComponent();

UART.BaudRate = 9600;

UART.PortName = "COM1";

UART.Open();

private void btn_bat_Click(object sender, EventArgs e)

UART.Write("1");

private void btn_tat_Click(object sender, EventArgs e)

{
UART.Write("2");

using System;

using System.Collections.Generic;

using System.Linq;

using System.Threading.Tasks;

using System.Windows.Forms;

namespace WindowsFormsApplication1

static class Program

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main()

Application.EnableVisualStyles();

Application.SetCompatibleTextRenderingDefault(false);

Application.Run(new Form1());

}
Bài 2:

void nap_bcd(char BCD);

#define _7447_A 6

#define _7447_B 5

#define _7447_C 4

#define _7447_D 3

#define max_buffer 17

char Buffer[max_buffer];

char Heso=0;

char dulieu='\0';

char so_dat;

boolean kiemtra=0;

void serialEvent() {

while (Serial.available()) {

// get the new byte:

char inChar = (char)Serial.read();


// add it to the inputString:

if(inChar!='\n')

Buffer[Heso]= inChar-48;

Heso++;

if(Heso>=max_buffer)

Heso=0;

else

so_dat=Buffer[0];

for(Heso=0;Heso<max_buffer;Heso++)

Buffer[Heso]='\n';

Heso=0;

// so_dat=inChar-48;

}
void setup() {

// put your setup code here, to run once:

Serial .begin(9600);

delay(100);

pinMode(_7447_A,OUTPUT);

pinMode(_7447_B,OUTPUT);

pinMode(_7447_C,OUTPUT);

pinMode(_7447_D,OUTPUT);

void loop() {

// put your main code here, to run repeatedly:

byte i=0;

nap_bcd(so_dat);

delay(100);

Serial.println(so_dat);

// for(i=0;i<=9;i++)

// {

// nap_bcd(i);

// delay(100);

// }

// delay(100);

// switch(dulieu)

// {
// case 'A':

// {

// for(i=0;i<=9;i++)

// {

// nap_bcd(i);

// delay(100);

// dulieu='\0';

// }

// dulieu='\0';

// break;

// }

// case 'B':

// {

// for(i=9;i>=0;i--)

// {

// nap_bcd(i);

// delay(100);

// dulieu='\0';

// }

// dulieu='\0';

// break;

// }

// default:

// {

// dulieu='\0';
// }

// }

void nap_bcd(char BCD)

switch(BCD)

case 0:

digitalWrite(_7447_A,LOW);

digitalWrite(_7447_B,LOW);

digitalWrite(_7447_C,LOW);

digitalWrite(_7447_D,LOW);

break;

case 1:

digitalWrite(_7447_A,HIGH);

digitalWrite(_7447_B,LOW);

digitalWrite(_7447_C,LOW);

digitalWrite(_7447_D,LOW);

break;

case 2:
{

digitalWrite(_7447_A,LOW);

digitalWrite(_7447_B,HIGH);

digitalWrite(_7447_C,LOW);

digitalWrite(_7447_D,LOW);

break;

case 3:

digitalWrite(_7447_A,HIGH);

digitalWrite(_7447_B,HIGH);

digitalWrite(_7447_C,LOW);

digitalWrite(_7447_D,LOW);

break;

case 4:

digitalWrite(_7447_A,LOW);

digitalWrite(_7447_B,LOW);

digitalWrite(_7447_C,HIGH);

digitalWrite(_7447_D,LOW);

break;

case 5:

{
digitalWrite(_7447_A,HIGH);

digitalWrite(_7447_B,LOW);

digitalWrite(_7447_C,HIGH);

digitalWrite(_7447_D,LOW);

break;

case 6:

digitalWrite(_7447_A,LOW);

digitalWrite(_7447_B,HIGH);

digitalWrite(_7447_C,HIGH);

digitalWrite(_7447_D,LOW);

break;

case 7:

digitalWrite(_7447_A,HIGH);

digitalWrite(_7447_B,HIGH);

digitalWrite(_7447_C,HIGH);

digitalWrite(_7447_D,LOW);

break;

case 8:

digitalWrite(_7447_A,LOW);
digitalWrite(_7447_B,LOW);

digitalWrite(_7447_C,LOW);

digitalWrite(_7447_D,HIGH);

break;

case 9:

digitalWrite(_7447_A,HIGH);

digitalWrite(_7447_B,LOW);

digitalWrite(_7447_C,LOW);

digitalWrite(_7447_D,HIGH);

break;

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

using System.IO.Ports;
namespace WindowsFormsApplication1

public partial class Form1 : Form

int dem = 0;

uint tam;

#region quan_li_ham

private void khoi_tao()

cbxTencom.DataSource = SerialPort.GetPortNames();

if (cbxTencom.Items.Count > 1)

cbxTencom.SelectedIndex = 1;

#endregion

#region quam_li_form

public Form1()

InitializeComponent();

private void Form1_Load(object sender, EventArgs e)

Control.CheckForIllegalCrossThreadCalls = false;
khoi_tao();

private void Form1_Shown(object sender, EventArgs e)

private void Form1_FormClosing(object sender, FormClosingEventArgs e)

private void Form1_KeyDown(object sender, KeyEventArgs e)

switch (e.KeyCode)

case Keys.Escape:

Close();

break;

default:

break;

}
}

#endregion

private void btn_ketnoi_Click(object sender, EventArgs e)

try

UART.BaudRate = 9600;

UART.PortName=cbxTencom.Text;

UART.Open();

timer1.Enabled = true;

if (UART.IsOpen == true)

MessageBox.Show("ket noi thanh cong");

catch (Exception ex)

MessageBox.Show(ex.Message);

private void btn_ngatketnoi_Click(object sender, EventArgs e)

try
{

UART.Close();

timer1.Enabled = false;

if (UART.IsOpen == false)

MessageBox.Show("ket noi khong thanh cong");

catch (Exception ex)

MessageBox.Show(ex.Message);

private void btn_send_Click(object sender, EventArgs e)

if (UART.IsOpen == true)

uint temp = Convert.ToUInt32(tx_send.Text.Trim());

UART.Write(tx_send.Text.Trim() + "\n");

dem = 0;

private void button1_Click(object sender, EventArgs e)

{
dem = 1;

private void button2_Click(object sender, EventArgs e)

dem = 2;

private void timer1_Tick(object sender, EventArgs e)

if (dem == 1)

tam = Convert.ToUInt32(tx_send.Text.Trim());

tam++;

if (tam > 0 && tam <= 9)

tx_send.Text= tam.ToString();

UART.Write(tx_send.Text.Trim()+ "\n");

if (tam > 9)

tam = 1;

else if (dem == 2)

{
tam = Convert.ToUInt32(tx_send.Text.Trim());

tam--;

if (tam > 0 && tam <= 9)

tx_send.Text = tam.ToString();

UART.Write(tx_send.Text.Trim() + "\n");

if (tam <= 0)

tam = 9;

else

dem = 0;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Threading.Tasks;

using System.Windows.Forms;
namespace WindowsFormsApplication1

static class Program

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main()

Application.EnableVisualStyles();

Application.SetCompatibleTextRenderingDefault(false);

Application.Run(new Form1());

}
Bài 3:

#include "HC595.h"

HC595 _HC595_LED;

HC595 _HC595_COT;

#define cambien1 A0

#define cambien2 A1

#define cambien3 A2

#define RESET A3

byte cacso[]={0,1,2,3,4,5};

char chuoi1[50];

char sothutu1=0,sothutu2=0,sothutu3=0;

void _Tach2so(byte dem,byte *chuc,byte *donvi);

void Hienthiled(byte Cacso[]);

void Scandelay(byte CacSo[],int t);

void tathetled(void);

void setup() {

// put your setup code here, to run once:

Serial.begin(9600);

delay(100);

_HC595_LED._KhoiDongHC595(7,8,9);

_HC595_COT._KhoiDongHC595(4,5,6);

pinMode(cambien1,INPUT);

pinMode(cambien2,INPUT);

pinMode(cambien3,INPUT);
pinMode(RESET,INPUT);

tathetled();

void loop() {

// put your main code here, to run repeatedly:

while(1)

if(digitalRead(cambien1)==0)

while(digitalRead(cambien1)==0)

Hienthiled(cacso);

sothutu1=sothutu1+1;

if(digitalRead(cambien2)==0)

while(digitalRead(cambien2)==0)

Hienthiled(cacso);

sothutu2=sothutu2+1;

if(digitalRead(cambien3)==0)
{

while(digitalRead(cambien3)==0)

Hienthiled(cacso);

sothutu3=sothutu3+1;

if(digitalRead(RESET)==0)

while(digitalRead(RESET)==0)

Hienthiled(cacso);

sothutu3=0;

sothutu2=0;

sothutu1=0;

sprintf(chuoi1,"%02d,%02d,%02d",sothutu1,sothutu2,sothutu3);

Serial.println(chuoi1);

_Tach2so(sothutu1,&cacso[0],&cacso[1]);

_Tach2so(sothutu2,&cacso[2],&cacso[3]);

_Tach2so(sothutu3,&cacso[4],&cacso[5]);

Scandelay(cacso,100);

}
}

void _Tach2so(byte dem,byte *chuc,byte *donvi)

*donvi=dem%10;

dem=dem/10;

*chuc=dem%10;

void Hienthiled(byte Cacso[])

byte i;

char Mang[8];

const byte
MaLed[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0xFF};//neu
la A chung

//byte
MaLed[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x00};//neu
la K chung

byte Madich=0x01;

for(i=0;i<=7;i++)

Mang[i]=MaLed[Cacso[i]];

for(i=0;i<=7;i++)

_HC595_COT._TruyenByte(Madich);
_HC595_LED._TruyenByte(Mang[i]);

delay(1);

_HC595_COT._TruyenByte(0x00);

Madich=Madich<<1;

void Scandelay(byte CacSo[],int t)

t=t/6;

while(t>0)

t=t-1;

Hienthiled(CacSo);

delay(1);

void tathetled(void)

_HC595_COT._TruyenByte(0x00);

_HC595_LED._TruyenByte(0x00);

using System;

using System.Collections.Generic;
using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

using System.IO.Ports;

namespace WindowsFormsApplication1

public partial class Form1 : Form

#region quan_li_ham

private void khoitao()

cbcTencom.DataSource = SerialPort.GetPortNames();

if (cbcTencom.Items.Count > 1)

cbcTencom.SelectedIndex = 1;

#endregion

#region quan_li_form
public Form1()

InitializeComponent();

private void Form1_Load(object sender, EventArgs e)

Control.CheckForIllegalCrossThreadCalls = false;

khoitao();

private void Form1_FormClosing(object sender,


FormClosingEventArgs e)

private void Form1_Shown(object sender, EventArgs e)

private void Form1_KeyDown(object sender, KeyEventArgs e)

switch (e.KeyCode)
{

case Keys.Escape:

Close();

break;

default:

break;

#endregion

private void button1_Click(object sender, EventArgs e)

try

UART.BaudRate = 9600;

UART.PortName = cbcTencom.Text;

UART.Open();

if (UART.IsOpen == true)

{
MessageBox.Show("ket noi thanh cong");

catch (Exception ex)

MessageBox.Show(ex.Message);

private void button2_Click(object sender, EventArgs e)

try

UART.Close();

if (UART.IsOpen == false)

MessageBox.Show("ket noi khong thanh cong");

catch (Exception ex)

{
MessageBox.Show(ex.Message);

string temp = "";

private void UART_DataReceived(object sender,


SerialDataReceivedEventArgs e)

temp = UART.ReadTo("\n");

var tach = temp.Split(',');

if (tach.Length >= 3)

tx_may1.Text = tach[0].ToString();

tx_may2.Text = tach[1].ToString();

tx_may3.Text = tach[2].ToString();

}
using System;

using System.Collections.Generic;

using System.Linq;

using System.Threading.Tasks;

using System.Windows.Forms;

namespace WindowsFormsApplication1

static class Program

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main()

Application.EnableVisualStyles();

Application.SetCompatibleTextRenderingDefault(false);

Application.Run(new Form1());

You might also like