Single chip microcomputer course design - waveform generator

Posted by Aldark on Thu, 20 Jan 2022 11:38:02 +0100

preface

  this article is a single chip microcomputer course design written by myself. If you send this article, you should leave a small souvenir; If there is anything bad, please point out
  the code of Keil C51 and the simulated Baidu network disk link of Proteus are put here
  link: Point me jump point me point me point me point me
  (extraction code: TYWL)

Tip: the following is the main content of this article. The following cases can be used for reference

1, What is the task of the course?

  1. Design a waveform generator that can produce more than three waveforms
  2. Design waveform selection button (3 independent keys)
  3. Dot matrix display waveform pattern;
  4. It can output two waveforms at the same time;
  5. Display frequency.

2, How to solve these tasks?

  1. It can produce more than 3 waveforms
      analysis: through consulting the data, we know that the commonly used waveforms include sine wave, square wave, sawtooth wave and triangular wave, and the rest are called arbitrary wave. Therefore, my main purpose is to output four waveforms
  2. It can output two waveforms at the same time
      when outputting two waveforms at the same time, it is necessary to use the same 8 I/O ports to take the waveform code and output it to the digital to analog conversion circuit, that is, time division multiplexing of port P0
      for waveform code retrieval, a two-dimensional array is used to store data
  3. Design waveform selection button (3 independent keys)
      Hardware: outputting two waveforms at the same time means that two waveforms need to be regulated and two buttons are required; There is one key to start setting waveform and one key to end setting waveform. There are four keys in total to select waveform
    Program: the initial idea is not to call the keyboard scan in the main function, but to call it in the external interrupt 0 service program. The query can save most resources when interrupted. Outputting two waveforms at the same time means that two waveforms need to be regulated and two keys are required Then define two variables, one key corresponds to one variable
  4. Dot matrix display waveform pattern
      Hardware: the smallest dot matrix in Proteus is 8 * 8 dot matrix. If it is directly connected to the I/O port on 51 single chip microcomputer, it is certainly not enough, so the serial port output mode is adopted The serial parallel conversion chip adopts 74HC595; Only 6 pins are needed to control the 8 * 8 dot matrix of 16 pins
      program: use a two-dimensional array to store the data displayed by the dot matrix; In order to save the space of C51, two variables are used to control the two-dimensional array
  5. Display frequency
      Hardware: P1 port is connected to 8 data ports of LCD1602, and other command bits are in P2 6 P2. seven
      program: the first line (i.e. write_com(0x80)) displays the waveform at the waveform output terminal 1 The second line (write_com(0xc0)) shows the frequency

3, Simulation schematic diagram

4, Code

  1. LCD1602 code
      for LCD1602, put its code and main function in the same place c. It will be very cumbersome in the document; For this waveform generator, LCD display is only an auxiliary function, so it is a good choice to put it in a separate header file
/*LCD1602.h LCD1602 header file*/
#ifndef _LCD1602_H_
#define _LCD1602_H_
#include<REG52.H>
#define uint  unsigned int
#define uchar unsigned char
/*LCD1602 Bit definition*/
sbit lcdrs = P2^7;//Rs: 0 = input command; 1 = input data
sbit lcden = P2^6;

/*LCD1602 Function declaration*/
void lcd_ram();					//Writing eight bytes of dot matrix data to LCDRAM will form a character
void init_lcd();				//Initialization function
void write_com(uchar com);		//Write command function
void write_date(uchar date);	//Write data function
void delay_LCD56(uint xms);
/*LCD1602 Characters to display*/
uchar code table[];	//1-9
uchar code table1[];//Front is Fout = 0-9
uchar code zifu[];
#endif

Relevant LCD1602 The C file is in the link, so it will not be displayed here

  1. Waveform output and 8*8LED display code
      for waveform output, the timer interrupt mode is adopted. The frequency of waveform can be controlled by setting the initial value of timing / counter, which is a good choice for future function expansion;
void T0_time() interrupt 1
{
	TH0=a;//Reload initial value
	TL0=b;
	u++;	//u self plus 1
	if(u>=64)	//If u exceeds 64, it will be reset to zero
		u=0;	//This is because there are only 64 values for each waveform

	/*Start outputting the first waveform*/
	WR1 = 1;
	CS0 = 0;			 /*CS0 Active low level, select the first DAC0832*/
	CS1 = 1;			 /*CS1 The low level is valid, and the second DAC0832 is not selected*/
	P0 = wave[flag_0][u];/*The data is output to the digital to analog conversion circuit through port P0*/
	WR1 = 0;
	delay(20);
	/*The first waveform output is completed*/
	
	/*8*8 The matrix displays the first waveform*/
	for(temp = 0;temp<4;temp++)
	{
		line_scan();
		send_595(matrix[flag_0][temp]);
		delay(3);
		ST_CP_0 = 0;
		ST_CP_0 = 1;	//On the rising edge, the storage register becomes high
		_nop_();
		ST_CP_0 = 0;	//Finished sending
	}
	/*8*8 The matrix shows that the first waveform is complete*/

	/*Start outputting the second waveform*/
	WR1 = 1;
	CS0 = 1;
	CS1 = 0;
	P0 = wave[flag_1][u];
	WR1 = 0;
	delay(20);
	/*The second waveform output is completed*/
	/*8*8 The matrix displays the second waveform*/
	for(temp = 0;temp<4;temp++)
	{
		line_scan();
		send_595(matrix[flag_1][temp]);
		delay(3);
		ST_CP_0 = 0;
		ST_CP_0 = 1;	//On the rising edge, the storage register becomes high
		_nop_();
		ST_CP_0 = 0;	//Finished sending
	}
	/*8*8 The matrix shows that the second waveform is completed*/
}

  1. Waveform selection
    Waveform selection uses external interrupt 0 to save resources; Set the priority higher than timer interrupt 0, and set the service program that can interrupt 0
void keyscan()
{
   //When the first key is pressed
   if(s1 == 0)
   {
   	delay(1);
   	if(s1 == 0)
   	{
   		EA = 0;				//Turn off total interrupt
   		while(!s1);		//Wait for the key to be released
   		if(++flag_0==4)
   			flag_0=0;
   		display();		//Display function
   		EA = 1;				//On total interrupt
   	}
   }
   //When the second key is pressed
   if(s2==0)			
   {	 
   	delay(1);			//debounce 
   	if(s2==0)			//Press the confirm button
   	{
   		EA=0;			//Turn off total interrupt
   		while(!s2);		//Wait for the key to release
   		if(++flag_1==4)
   			flag_1=0;
   		EA=1;
   	}
   }	
}
void interrupt_0() interrupt 0 using 3
{
   while (s3!=0)
   //Wait for the S3 key to be pressed, jump out of the cycle and end the interrupt service program
   {
   		keyscan();
   }
}

ending

If there is any error, please point it out in a private letter

Topics: Single-Chip Microcomputer