天天看點

如何快速學會單片機程式設計并應用?

如何快速學會單片機程式設計并應用?

先上一些參考資料,主要來源知乎:

1 -怎樣學會單片機?-

2 -arduino、arm、樹莓派、單片機四者有什麼不同?-

3 -單片機可以替代PLC麼?-

4 -單片機有沒有必要用彙編講?-

5 -相關課程-

單片機和C語言,是自動化(機器人)學科重要的基礎内容。

如果對機器人感興趣,可參考機器人工程師學習計劃。

課程學習動機~Why?為什麼學習單片機程式設計?

單片機方向就業?把握市場需求!

軟硬體能力的綜合訓練,電路原理圖+軟體程式設計(C語言)。

課程學習内容~What?單片機程式設計包括哪些内容?

目錄和大綱,歸納和總結能力訓練

如何快速學會單片機程式設計并應用?

課程學習方法~How?如何學習單片機程式設計?

在掌握基礎知識後,仿真與實驗。

Linux平台:MCU 8051 IDE

Windows平台:uVision+Proteus

擴充提升:在學完51單片機後,能夠快速自學更為通用主流的嵌入式系統,如下:

C51--(Arduino、MSP430)--(2812、28335)--(STM32、ARM9)--(TK1、BeagleBone、Raspberry Pi)

那麼問題來了,我們為什麼不直接學習STM32等,而是要學習51呢?

入門簡單、上手容易,欲知詳情點選檢視。

其他參考資料:

單片機技術理論與實踐課程相關資料,課件、資料和工具軟體等。

-新更新考核材料和參考報告-

程式設計語言。

單片機泥石流負能量!

在美國大學計算機專業都學什麼?

2016 年最受歡迎的程式設計語言是什麼?

憑興趣求職80%會失敗,為什麼?

視訊短片:

STEM教育 1 2 3 | ROS | 智慧家居 | 智能駕駛

子產品化,低耦合 參考軟體工程學

示例1 51+arduino

#include<reg51.h>       //寄存器定義
  #include<stdio.h>       //一般I/O口定義
  /***以下是全局變量定義*********/
  sbit LED=P1^0;      //LED燈連接配接在P1.0上
  int data i;         //定義一個整型全局變量
  /*********主程式開始***************/
  void  main(void)
  {  while(1)
     {  LED=0;     //LED燈點亮
        for(i=0;i<1000;i++);   //延時
        LED=1;           //LED燈熄滅
        for(i=0;i<1000;i++);   //延時
      }
  }


  #include<reg51.h>       //寄存器定義
  #include<stdio.h>       //一般I/O口定義
  /***以下是全局變量定義*********/
  sbit LED=P1^0;      //LED燈連接配接在P1.0上
  int data i;         //定義一個整型全局變量
  LED_demo()       //LED函數
  {     LED=0;     //LED燈點亮
        for(i=0;i<1000;i++);   //延時
        LED=1;           //LED燈熄滅
        for(i=0;i<1000;i++);   //延時  
  }
  /*********主程式開始***************/
  void  main(void)
  {  while(1)
     {  
     	 LED_demo();
      }
  }
  
/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.

  Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO 
  it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to
  the correct LED pin independent of which board is used.
  If you want to know what pin the on-board LED is connected to on your Arduino model, check
  the Technical Specs of your board  at https://www.arduino.cc/en/Main/Products
  
  This example code is in the public domain.

  modified 8 May 2014
  by Scott Fitzgerald
  
  modified 2 Sep 2016
  by Arturo Guadalupi
  
  modified 8 Sep 2016
  by Colby Newman
*/


// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}      

示例2 51+arduino

#include<reg52.h>					//預處理指令,reg52.h是一個頭檔案
#include<stdio.h>
void Function1(void);				//自定義函數Function1聲明
unsigned int ch;//全局變量聲明

void main(void)					//主函數
{


	SCON=0x50;               		//SCON:模式1,8bit異步序列槽通信
	TMOD=0x20;               		//TMOD:定時器1為模式2,8bit自動裝載方式
	TH1=221;                 		//TH1:1200bit/s的裝載值,16MHz
	TR1=1;                   		//TR1:timer1運作
	TI=1;                    		//TI:設定為1,以發送第一個位元組

	while(ch<=5)
	{ 
		Function1( );//調用自定義函數
		printf("char=%d
",ch);//程式語句
	}
	while(1);
}
void Function1(void)				//自定義函數Function1
{	
	unsigned char ps;				//自定義函數内部變量聲明
	ps=1;
	ch=ch+ps;
}


#include<reg52.h>					//預處理指令,reg52.h是一個頭檔案
#include<stdio.h>
void Function1(void);				//自定義函數Function1聲明
void Init1(void);
unsigned int ch;//全局變量聲明

void main(void)					//主函數
{
	Init1();
	while(ch<=5)
	{ 
		Function1( );//調用自定義函數
		printf("char=%d
",ch);//程式語句
	}
	while(1);
}
void Function1(void)				//自定義函數Function1
{	
	unsigned char ps;				//自定義函數内部變量聲明
	ps=1;
	ch=ch+ps;
}
void Init1(void)
{


	SCON=0x50;               		//SCON:模式1,8bit異步序列槽通信
	TMOD=0x20;               		//TMOD:定時器1為模式2,8bit自動裝載方式
	TH1=221;                 		//TH1:1200bit/s的裝載值,16MHz
	TR1=1;                   		//TR1:timer1運作
	TI=1;                    		//TI:設定為1,以發送第一個位元組
}


/*
  Serial Call and Response in ASCII
 Language: Wiring/Arduino

 This program sends an ASCII A (byte of value 65) on startup
 and repeats that until it gets some data in.
 Then it waits for a byte in the serial port, and
 sends three ASCII-encoded, comma-separated sensor values,
 truncated by a linefeed and carriage return,
 whenever it gets a byte in.

 Thanks to Greg Shakar and Scott Fitzgerald for the improvements

  The circuit:
 * potentiometers attached to analog inputs 0 and 1
 * pushbutton attached to digital I/O 2



 Created 26 Sept. 2005
 by Tom Igoe
 modified 24 Apr 2012
 by Tom Igoe and Scott Fitzgerald

 This example code is in the public domain.

 http://www.arduino.cc/en/Tutorial/SerialCallResponseASCII

 */

int firstSensor = 0;    // first analog sensor
int secondSensor = 0;   // second analog sensor
int thirdSensor = 0;    // digital sensor
int inByte = 0;         // incoming serial byte

void setup() {
  // start serial port at 9600 bps and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  pinMode(2, INPUT);   // digital sensor is on digital pin 2
  establishContact();  // send a byte to establish contact until receiver responds
}

void loop() {
  // if we get a valid byte, read analog ins:
  if (Serial.available() > 0) {
    // get incoming byte:
    inByte = Serial.read();
    // read first analog input:
    firstSensor = analogRead(A0);
    // read second analog input:
    secondSensor = analogRead(A1);
    // read  switch, map it to 0 or 255L
    thirdSensor = map(digitalRead(2), 0, 1, 0, 255);
    // send sensor values:
    Serial.print(firstSensor);
    Serial.print(",");
    Serial.print(secondSensor);
    Serial.print(",");
    Serial.println(thirdSensor);
  }
}

void establishContact() {
  while (Serial.available() <= 0) {
    Serial.println("0,0,0");   // send an initial string
    delay(300);
  }
}

      

未完待續