Documente Academic
Documente Profesional
Documente Cultură
Incorporate
Proiect
Realizarea unei versiuni
simplificate
a celebrului joc Tetris
Elevi :
Enuntul proiectului
si a circuitului
dedicat utilizat pentru realizarea
proiectului.
1. ATmega16A
Acesta are 32 intrari/iesiri digitale , un oscilator la 16MHz, o conexiune pe USB, o
mufa de alimentare si un buton de reset. Consta intr-o platforma de mici dimensiuni (6.8
cm / 5.3 cm in cea mai des intalnita varianta) construita in jurul unui procesor de
semnal si este capabila de a prelua date din mediul inconjurator printr-o serie de
senzori si de a efectua actiuni asupra mediului prin intermediul luminilor, motoarelor,
servomotoare, si alte tipuri de dispozitive mecanice.
Procesorul este capabil sa ruleze cod scris intr-un limbaj de programare care este
foarte similar cu limbajul C++.
Descrierile pinilor
Nume
VCC
GND
Port A
(PA7:PA0)
Port B
(PB7:PB0)
Port C
(PC7:PC0)
Port D
(PD7:PD0)
/RESET
XTAL1
XTAL2
AVCC
AREF
Descriere
Digital supply voltage.
Ground.
Portul A servete ca intrre analogica la A / D Converter. Portul
A servete, de asemenea, ca un port I/O de 8 bii bidirecional n
cazul n care convertor A / D nu este utilizat.
Port B este un port I/O de 8 bii bidirecionali cu rezistene
interne pull-up (selecionat pentru fiecare bit). Pinii portului B
sunt tri-declarati atunci cnd o condiie de resetare devine
active , chiar dac clockul nu se execut.
Port C este un port I/O de 8 bii bidirecionali .
Port D este un port I/O de 8 bii bidirecionali . Tampoanele de
iesire ale portului D au caracteristici de actionare simetrice atat
cu capacitatea surs cat si cu high sink .
Reset Input . Un nivel sczut pe acest pin mai mult dect
lungimea minim de puls va genera o resetare , chiar dac
clockul nu se execut .
Intrare pentru amplificatorul inversor i intrarea pentru
clockul circuitul de operare .
Iesire din Oscilator.
AVCC este PIN-ul de tensiune de alimentare pentru Port A iA / D
Converter . Trebuie conectat extern la VCC , chiar
dac ADC nu este folosit . Dac se utilizeaz ADC , ar trebui s
fie conectat la VCC printr-un filtru low-pass .
AREF este PIN-ul de referin analogica pentru A / D Converter.
3.Bread Board
Un Breadboard, numit si protoboard, reprezinta o baza de
constructie pentru realizarea de prototipuri in electronica.
Termenul de breadboard este deobicei utilizat pentru a face
referire la faptul ca acest gen de placa nu necesita lipituri pentru
a realiza conexiunea elementelor (fiind de genul plugboard).
Datorita faptului ca elementele nu trebuie lipite pe placa, aceasta
poate fi refolosita. Acest lucru faciliteaza crearea de noi
prototipuri temporare si de diferite design-uri de circuite.
Un breadboarb consta dintr-un bloc perforat care are clipuri de
staniu sau bronz fosforos sau nichel sub perforatii . Clipurile sunt
4.Multiplexorul DG409
DG409 este un multiplexor analogic cu 8 canale facut
pentru a conecta una dintre cele opt intrri la o ieire comun
determinat printr-o adres pe 2 bii (A0, A1).
Canalul ON conduce la fel de bine curentul in ambele directii
la fel de bine . In starea off fiecare canal blocheaza voltajul . O
functie (EN) permite utilizatorului sa reseteze multiplexor /
demultiplexorul . Toate intrrile de control, adresa (Ax)
i enable (EN) sunt compatibile TTL.
Schema bloc :
Tabelul de adevar :
A2
X
0
0
1
1
A1
x
0
1
0
1
A0
X
0
1
0
1
EN
0
1
1
1
1
ON
SWITCH
None
1
2
3
4
7.
Pin
Name
Description
TXD
DTR_N
RTS_N
VDD_325
RS232 VDD. The power pin for the serial port signals. When
the serial port is 3.3V, this should be 3.3V. When the serial port
is 2.5V, this should be 2.5V. The range can be from 1.8V~3.3V.
RXD
6
7
RI_N
GND
Ground
NC
No Connection
DSR_N
DCD_N
Serial Port (Clear to Send); or Auxiliary General Purpose I/O
11
CTS_N
12
SHTD_N
13
GP3
14
GP2
15
DP
16
DM
17
VO_33
18
GND
Ground
19
RESET_N
20
VDD_5
21
NC
No Connection
22
GP0
23
GP1
24
NC
No Connection
25
GND
Ground
26
TEST
27
NC
No Connection
28
Reserved
C. Cod
#include
#include
#include
#include
"algorithm.h"
"matrix.h"
"figure.h"
"UART.h"
U8_T speed = 0;
S8_T row = 0;
S8_T column = 0;
S8_T last_row = 0;
S8_T last_column = 0;
U8_T fig_init = 0;
U16_T counter = 0;
U16_T timer = SPEED; //10000 ~ 1 sec
U16_T score = 0;
U8_T figure_number = 0;
U8_T figure_position = 0;
U8_T matrix[16][8] = {{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}};
U8_T figure[4][4] = {{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}};
void process_game()
{
counter++;
if (fig_init == 0)
{
figure_init();
}
if (counter > timer)
{
if (timer > 2000)
{
timer = timer - 20;
}
if (fig_init == 1)
{
USART_WriteChar(score);
figure_change_position(nothing);
}
}
if (counter % 5 == 0)
{
matrix_clear();
matrix_display();
}
}
U8_T figure_random()
{
return (rand()+1+figure_number)%4;
}
void figure_copy(U8_T fig[4][4])
{
U8_T i = 0;
U8_T j = 0;
for (i=0; i<4; i++)
{
for (j=0; j<4; j++)
{
figure[i][j] = fig[i][j];
}
}
}
void figure_init()
{
if (fig_init == 0)
{
USART_WriteChar(score);
figure_number = figure_random();
figure_position = 0;
fig_init = 1;
row = -1;
column = 3;
last_row = -1;
last_column = 3;
}
switch(figure_number)
{
case 0:
{
switch(figure_position)
{
case 0:
{
figure_copy(figure1A);
break;
}
case 1:
{
figure_copy(figure1B);
break;
}
case 2:
{
figure_copy(figure1C);
break;
}
case 3:
{
figure_copy(figure1D);
break;
}
}
break;
}
case 1:
{
switch(figure_position)
{
case 0:
{
figure_copy(figure2A);
break;
}
case 1:
{
figure_copy(figure2B);
break;
}
case 2:
{
figure_copy(figure2C);
break;
}
case 3:
{
figure_copy(figure2D);
break;
}
}
break;
}
case 2:
{
switch(figure_position)
{
case 0:
{
figure_copy(figure3A);
break;
}
case 1:
{
figure_copy(figure3B);
break;
}
case 2:
{
figure_copy(figure3C);
break;
}
case 3:
{
figure_copy(figure3D);
break;
}
}
break;
}
case 3:
{
switch(figure_position)
{
case 0:
{
figure_copy(figure4A);
break;
}
case 1:
{
figure_copy(figure4B);
break;
}
case 2:
{
figure_copy(figure4C);
break;
}
case 3:
{
figure_copy(figure4D);
break;
}
}
break;
}
default:
{
}
}
}
void matrix_figure_clear()
{
S8_T i = 0;
S8_T j = 0;
for (i=last_row; i>=last_row-3 && i>=0; i--)
{
for (j=last_column; j<=last_column+3 && j<=7; j++)
{
if (figure[3-(last_row-i)][j-last_column] == 1)
{
matrix[i][j] = 0;
}
}
}
}
void matrix_figure_fill()
{
S8_T i = 0;
S8_T j = 0;
last_row = row;
last_column = column;
{
complete = 0;
}
}
if (complete == 1)
{
k = i;
while (k>0)
{
for (j=0; j<8; j++)
{
matrix[k][j] = matrix[k-1][j];
}
k--;
}
i = 15;
score = score + 10;
}
else
{
i--;
}
}
}
void figure_change_position(U8_T position)
{
switch (position)
{
case 255: //do nothing
{
}
case nothing: // nothing pressed
{
if (counter > timer)
{
counter = 0;
S8_T i = 0;
U8_T j = 0;
U8_T forbidden = 0;
matrix_figure_clear();
i++;
matrix_figure_clear();
matrix_figure_fill();
}
}
fig_init = 0;
matrix_full_row_check();
figure_init();
break;
}
case left: // pressed left
{
S8_T i = 0;
S8_T j = 0;
U8_T forbidden = 0;
if (column > 0)
{
j = column - 1;
for (i=row; i>=row-3 && i>=0; i--)
{
if ((matrix[i][j] == 1) && (figure[3-(row-i)][0] == 1))
{
forbidden = 1;
}
}
if (forbidden == 0)
{
column--;
matrix_figure_clear();
matrix_figure_fill();
}
}
break;
}
case right: // pressed right
{
S8_T i = 0;
S8_T j = 0;
U8_T forbidden = 0;
U8_T fig_last_column = 0;
for (j=3; j>0; j--)
{
matrix_figure_clear();
figure_init();
matrix_figure_fill();
break;
}
case reset:
{
timer = SPEED;
score = 0;
matrix_reset();
break;
}
default:
{
timer = SPEED;
score = 0;
matrix_reset();
break;
}
}
}
U8_T
U8_T
U8_T
U8_T
line1 = 0;
column1 = 0;
line2 = 8;
column2 = 0;
void matrix_display()
{
U8_T gasit1 = 0;
U8_T gasit2 = 0;
while (line1<8)
{
while ((column1<8) && (gasit1 == 0))
{
if (matrix[line1][column1] == 1)
{
activate_led_matrix1(column1 + 1, 8 - line1);
gasit1 = 1;
column1++;
break;
}
else
{
column1++;
}
}
if (gasit1 == 1)
{
break;
}
else
{
column1 = 0;
}
line1++;
}
if (line1 > 7)
{
line1 = 0;
}
while (line2<16)
{
while ((column2<8) && (gasit2 == 0))
{
if (matrix[line2][column2] == 1)
{
activate_led_matrix2(column2 + 1, 16 - line2);
gasit2 = 1;
column2++;
break;
}
else
{
column2++;
}
}
if (gasit2 == 1)
{
break;
}
else
{
column2 = 0;
}
line2++;
}
/*
* figure.h
*
* Created: 4/27/2015 10:04:27 PM
#include "common.h
#ifndef FIGURE_H_
#define FIGURE_H_
////////////////////////////////////////////////////////////////////////////////
U8_T figure1A[4][4] = {{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 1, 0},
{1, 1, 1, 0}};
U8_T figure1B[4][4] = {{0, 0, 0, 0},
{1, 0, 0, 0},
{1, 0, 0, 0},
{1, 1, 0, 0}};
U8_T figure1C[4][4] = {{0, 0, 0, 0},
{0, 0, 0, 0},
{1, 1, 1, 0},
{1, 0, 0, 0}};
U8_T figure1D[4][4] = {{0, 0, 0, 0},
{1, 1, 0, 0},
{0, 1, 0, 0},
{0, 1, 0, 0}};
////////////////////////////////////////////////////////////////////////////////
U8_T figure2A[4][4] = {{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 1, 0, 0},
{1, 1, 1, 0}};
U8_T figure2B[4][4] = {{0, 0, 0, 0},
{1, 0, 0, 0},
{1, 1, 0, 0},
{1, 0, 0, 0}};
U8_T figure2C[4][4] = {{0, 0, 0, 0},
{0, 0, 0, 0},
{1, 1, 1, 0},
{0, 1, 0, 0}};
U8_T figure2D[4][4] = {{0, 0, 0, 0},
{0, 1, 0, 0},
{1, 1, 0, 0},
{0, 1, 0, 0}};
////////////////////////////////////////////////////////////////////////////////
U8_T figure3A[4][4] = {{0, 0, 0, 0},
{0, 0, 0, 0},
{1, 1, 0, 0},
{0, 1, 1, 0}};
U8_T figure3B[4][4] = {{0, 0, 0, 0},
{0, 1, 0, 0},
{1, 1, 0, 0},
{1, 0, 0, 0}};
U8_T figure3C[4][4] = {{0, 0, 0, 0},
{0, 0, 0, 0},
{1, 1, 0, 0},
{0, 1, 1, 0}};
U8_T figure3D[4][4] = {{0, 0, 0, 0},
{1, 0, 0, 0},
{1, 1, 0, 0},
{0, 1, 0, 0}};
////////////////////////////////////////////////////////////////////////////////
U8_T figure4A[4][4] = {{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0},
{1, 1, 1, 1}};
U8_T figure4B[4][4] = {{1, 0, 0, 0},
{1, 0, 0, 0},
{1, 0, 0, 0},
{1, 0, 0, 0}};
U8_T figure4C[4][4] = {{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0},
{1, 1, 1, 1}};
U8_T figure4D[4][4] = {{1, 0, 0, 0},
{1, 0, 0, 0},
{1, 0, 0, 0},
{1, 0, 0, 0}};
////////////////////////////////////////////////////////////////////////////////
#endif /* FIGURE_H_ */
/*
* matrice.c
*
*/
#include "matrix.h"
void matrix_init()
{
DDRA = 0xFF;
DDRC = 0xFF;
PORTA = 0x00;
PORTC = 0x00;
}
void matrix_clear()
{
PORTA = 0x00;
PORTC = 0x00;
}
void activate_led_line_matrix1(U8_T line)
{
switch (line)
{
case 1:
{
PORTA |= (1 << VCC1A);
break;
}
case 2:
{
PORTA |= (1 << VCC1A) |
break;
}
case 3:
{
PORTA |= (1 << VCC1A) |
break;
}
case 4:
{
PORTA |= (1 << VCC1A) |
break;
}
case 5:
{
PORTA |= (1 << VCC1B);
break;
}
case 6:
{
PORTA |= (1 << VCC1B) |
(1 << VCC1A0);
(1 << VCC1A1);
(1 << VCC1A0);
break;
}
case 7:
{
PORTA |= (1 << VCC1B) | (1 << VCC1A1);
break;
}
case 8:
{
PORTA |= (1 << VCC1B) | (1 << VCC1A0) | (1 << VCC1A1);
break;
}
}
}
void activate_led_column_matrix1(U8_T column)
{
switch (column)
{
case 1:
{
PORTA |= (1 << GND1A);
break;
}
case 2:
{
PORTA |= (1 << GND1A) | (1 << GND1A0);
break;
}
case 3:
{
PORTA |= (1 << GND1A) | (1 << GND1A1);
break;
}
case 4:
{
PORTA |= (1 << GND1A) | (1 << GND1A0) | (1 << GND1A1);
break;
}
case 5:
{
PORTA |= (1 << GND1B);
break;
}
case 6:
{
PORTA |= (1 << GND1B) | (1 << GND1A0);
break;
}
case 7:
{
PORTA |= (1 << GND1B) | (1 << GND1A1);
break;
}
case 8:
{
PORTA |= (1 << GND1B) | (1 << GND1A0) | (1 << GND1A1);
break;
}
}
}
void activate_led_matrix1(U8_T line, U8_T column)
{
PORTA = 0x00;
activate_led_line_matrix1(line);
activate_led_column_matrix1(column);
}
void activate_led_line_matrix2(U8_T line)
{
switch (line)
{
case 1:
{
PORTC |= (1 << VCC2A);
break;
}
case 2:
{
PORTC |= (1 << VCC2A) | (1 << VCC2A0);
break;
}
case 3:
{
PORTC |= (1 << VCC2A) | (1 << VCC2A1);
break;
}
case 4:
{
PORTC
break;
}
case 5:
{
PORTC
break;
}
case 6:
{
PORTC
break;
}
case 7:
{
PORTC
break;
}
case 8:
{
PORTC
break;
}
|= (1 << VCC2B);
}
}
void activate_led_column_matrix2(U8_T column)
{
switch (column)
{
case 1:
{
PORTC |= (1 << GND2A);
break;
}
case 2:
{
PORTC |= (1 << GND2A) | (1 << GND2A0);
break;
}
case 3:
{
|= (1 << GND2B);
}
}
void activate_led_matrix2(U8_T line, U8_T column)
{
PORTC = 0x00;
activate_led_line_matrix2(line);
activate_led_column_matrix2(column);
}
Interfata din C#
Cod C# :
#region Namespace Inclusions
using System;
using System.Linq;
using System.Data;
using System.Text;
using System.Drawing;
using System.IO.Ports;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;
using SerialPortTerminal.Properties;
using System.Threading;
using System.IO;
#endregion
namespace SerialPortTerminal
{
#region Public Enumerations
public enum DataMode { Text, Hex }
public enum LogMsgType { Incoming, Outgoing, Normal, Warning, Error };
#endregion
public partial class frmTerminal : Form
{
#region Local Variables
#endregion
cmbStopBits.Text);
settings.DataMode = CurrentDataMode;
settings.Parity = (Parity)Enum.Parse(typeof(Parity), cmbParity.Text);
settings.StopBits = (StopBits)Enum.Parse(typeof(StopBits),
settings.PortName = cmbPortName.Text;
settings.ClearOnOpen = chkClearOnOpen.Checked;
settings.ClearWithDTR = chkClearWithDTR.Checked;
settings.Save();
/// <summary> Populate the form's controls with default settings. </summary>
private void InitializeControlValues()
{
cmbParity.Items.Clear(); cmbParity.Items.AddRange(Enum.GetNames(typeof(Parity)));
cmbStopBits.Items.Clear(); cmbStopBits.Items.AddRange(Enum.GetNames(typeof(StopBits)));
cmbParity.Text = settings.Parity.ToString();
cmbStopBits.Text = settings.StopBits.ToString();
cmbDataBits.Text = settings.DataBits.ToString();
cmbParity.Text = settings.Parity.ToString();
cmbBaudRate.Text = settings.BaudRate.ToString();
CurrentDataMode = settings.DataMode;
RefreshComPortList();
chkClearOnOpen.Checked = settings.ClearOnOpen;
chkClearWithDTR.Checked = settings.ClearWithDTR;
// If it is still avalible, select the last com port used
if (cmbPortName.Items.Contains(settings.PortName)) cmbPortName.Text =
settings.PortName;
else if (cmbPortName.Items.Count > 0) cmbPortName.SelectedIndex =
cmbPortName.Items.Count - 1;
else
{
MessageBox.Show(this, "There are no COM Ports detected on this computer.\nPlease install a
COM Port and restart this app.", "No COM Ports Installed", MessageBoxButtons.OK,
MessageBoxIcon.Error);
this.Close();
}
}
/// <summary> Enable/disable controls based on the app's current state. </summary>
private void EnableControls()
{
// Enable/disable controls based on whether the port is open or not
gbPortSettings.Enabled = !comport.IsOpen;
txtSendData.Enabled = btnSend.Enabled = comport.IsOpen;
//chkDTR.Enabled = chkRTS.Enabled = comport.IsOpen;
if (comport.IsOpen) btnOpenPort.Text = "&Close Port";
else btnOpenPort.Text = "&Open Port";
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Left)
txtSendData.Text = "2";
SendData();
return true;
}
if (keyData == Keys.Right)
{
txtSendData.Text = "3";
SendData();
return true;
}
if (keyData == Keys.Down)
{
txtSendData.Text = "1";
SendData();
return true;
}
if (keyData == Keys.Space)
{
txtSendData.Text = "4";
SendData();
return true;
}
if (keyData == Keys.R)
{
txtSendData.Text = "5";
SendData();
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
/// <summary> Send the user's data currently entered in the 'send' box.</summary>
private void SendData()
{
if (CurrentDataMode == DataMode.Text)
{
// Send the user's text straight out the port
comport.Write(txtSendData.Text);
// Show in the terminal window the user's text
Log(LogMsgType.Outgoing, txtSendData.Text + "\n");
}
else
{
try
{
// Convert the user's string of hex digits (ex: B4 CA E2) to a byte array
byte[] data = HexStringToByteArray(txtSendData.Text);
// Send the binary data out the port
comport.Write(data, 0, data.Length);
// Show the hex digits on in the terminal window
Log(LogMsgType.Outgoing, ByteArrayToHexString(data) + "\n");
}
catch (FormatException)
{
// Inform the user if the hex string was not properly formatted
Log(LogMsgType.Error, "Not properly formatted hex string: " + txtSendData.Text + "\n");
}
}
txtSendData.SelectAll();
}
}
}
#endregion
}
catch (UnauthorizedAccessException) { error = true; }
catch (IOException) { error = true; }
textBoxScore.Text = decValue.ToString();
}
}
private void txtSendData_KeyDown(object sender, KeyEventArgs e)
{
// If the user presses [ENTER], send the data now
if (KeyHandled = e.KeyCode == Keys.Enter) { e.Handled = true; SendData(); }
}
private void txtSendData_KeyPress(object sender, KeyPressEventArgs e)
{ e.Handled = KeyHandled; }
#endregion
private void chkDTR_CheckedChanged(object sender, EventArgs e)
{
comport.DtrEnable = chkDTR.Checked;
if (chkDTR.Checked && chkClearWithDTR.Checked) ClearTerminal();
}
private void chkRTS_CheckedChanged(object sender, EventArgs e)
{
comport.RtsEnable = chkRTS.Checked;
}
private void btnClear_Click(object sender, EventArgs e)
{
ClearTerminal();
}
private void ClearTerminal()
{
rtfTerminal.Clear();
}
private void tmrCheckComPorts_Tick(object sender, EventArgs e)
{
// checks to see if COM ports have been added or removed
// since it is quite common now with USB-to-Serial adapters
RefreshComPortList();
}
private void RefreshComPortList()
{
// Determain if the list of com port names has changed since last checked
string selected = RefreshComPortList(cmbPortName.Items.Cast<string>(),
cmbPortName.SelectedItem as string, comport.IsOpen);
// If there was an update, then update the control showing the user the list
of port names
if (!String.IsNullOrEmpty(selected))
{
cmbPortName.Items.Clear();
cmbPortName.Items.AddRange(OrderedPortNames());
cmbPortName.SelectedItem = selected;
}
}
private string[] OrderedPortNames()
{
// Just a placeholder for a successful parsing of a string to an integer
int num;
// Order the serial port names in numberic order (if possible)
return SerialPort.GetPortNames().OrderBy(a => a.Length > 3 &&
int.TryParse(a.Substring(3), out num) ? num : 0).ToArray();
}
private string RefreshComPortList(IEnumerable<string> PreviousPortNames, string
CurrentSelection, bool PortOpen)
{
// Create a new return report to populate
string selected = null;
// Retrieve the list of ports currently mounted by the operating system
(sorted by name)
if (PortOpen)
{
if (ports.Contains(CurrentSelection)) selected =
CurrentSelection;
}
else
{
}
// If there was a change to the port list, return the recommended default
selection
}
return selected;
Bibliografie