Skip to content
GitLab
Projects Groups Topics Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in
  • M mbed0
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributor statistics
    • Graph
    • Compare revisions
  • Issues 0
    • Issues 0
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 0
    • Merge requests 0
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Artifacts
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Container Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • 蕭瑤
  • mbed0
  • Wiki
  • Home

Home · Changes

Page history
Update home authored Mar 20, 2018 by 蕭瑤's avatar 蕭瑤
Show whitespace changes
Inline Side-by-side
home.md
View page @ f7096a97
# Table Content
* [mbed01](#mbed01)
* [mbed02](#mbed02)
* [mbed03](#mbed03)
* [mbed04](#mbed04)
## <a name="mbed01">mbed01-Digital Output and Input</a>
* `DigitalOut` -> create a DigitalOut object connected to specified pin
* `=` equiv. write
* `int()` equiv. read
* `DigitalIn` -> create a DigitalIn object connected to specified pin
* `operator int()` equiv. read
* `wait`
* **all numbered pins(PT_*) can also be used as DigitalIn and DigitalOut**
* `BusOut` is an object used for setting the state of a collection of pins. We use BusOut to avoid too many declarations.
* Seven-Sement-Displays
```c++
#include "mbed.h"
DigitalOut redLED(LED1);
//DigitalOut is a class which is used for setting the state of a pin like LED1 or LED2 and so on.
DigitalOut greenLED(LED2);
int main(){
while(1){
redLED = 1;
greenLED = 0;
wait(0.2);
redLED = 0;
greenLED = 1;
wait(0.2);
}
}
```
```c++
#include "mbed.h"
// The DigitalIn SW2 is a button on the FRDM-K64F. We will use this button to control the led.
// DigitalIn is a class which used for reading the state of a pin.
DigitalIn Switch(SW2);
DigitalOut redLED(LED1);
DigitalOut greenLED(LED2);
int main(){
while(1){
if( Switch == 1 ) {
greenLED = 0;
redLED = 1;
} else {
redLED = 0;
greenLED = 1;
}
}
}
```
```c++
#include "mbed.h"
BusOut display(D6, D7, D9, D10, D11, D5, D4, D8);
char table[10] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F};
int main(){
while(1){
for (int i = 0; i<10; i = i+1){
display = table[i];
wait(1);
}
}
}
```
## <a name="mbed02">mbed02-Analog Output</a>
* A digital-to-analog converter (DAC) converts a binary input number of n digits into an analog output.
`V_out = D(input)/pow(2,n) * V_ref`.
Step size, resolution, is `V_ref/total number of input`. Eg. 12-bit DAC, with V_ref of 3.3V -> step size = 3.3/2^12
* mbed's internal DAC has it output connect to mbed's single analog output, on pin *DAC0_OUT*
* `AnalogOut` creates an AnalogOut object connected to specified pin
* read
* write or '=': set output voltage, **takes floating point between 0.0 and 1.0. The actual output voltage on DAC0_OUT pin is between 0 V and 3.3 V**
```c++
#include "mbed.h"
AnalogOut Aout(DAC0_OUT);
int main(){
while(1){
Aout = 0.25; // 0.25 * 3.3 = 0.825 v
wait(2);
Aout = 0.5; // 0.50 * 3.3 = 1.650 v
wait(2);
Aout = 0.75; // 0.75 * 3.3 = 2.475 v
wait(2);
}
}
```
```c++
//sawtooth
#include "mbed.h"
AnalogOut Aout(DAC0_OUT);
int main(){
float i;
while(1){
for( i=0; i<1; i+=0.1 ){
Aout = i;
wait(0.001);
}
}
}
```
```c++
//sinewave
#include "mbed.h"
Analogout Aout(DAC0_OUT);
int main(){
float i;
while(1){
for( i=0; i<2; i+=0.05 ){
Aout = 0.5 + 0.5*sin(i*3.14159);
//sine value plus half the range
wait(0.001);
//Controls the sine wave period
}
}
}
```
* Pulse width modulation (PWM) is an alternative to the DAC by getting a rectangular digital waveform to control an analog variable, usually voltage or current. Duty cycle determines the average value
* The mbed has 12 PWM outputs. **Each has two variables, period and pulse width, or duty cycle derived from these.** Similar to previous examples, a PWM output can be established, named and allocated to a pin using `PwmOut`. `All PWM outputs share the same period/frequency.`
Duty cycle = pulse width / period * 100%
* `PwmOut` class creates a PwmOut object connected to the specified pin
* `write or =` set output duty-cycle(0~1)
* `.period()` sets period
```c++
#include "mbed.h"
PwmOut PWM1(D6);
int main() {
PWM1.period(0.01);
PWM1 = 0.5;
}
```
## <a name="mbed03">mbed03-Analog Input </a>
* An ADC is an electronic circuit whose digital output is proportional to its analog voltage input. Effectively it "measures" the input voltage, and gives a binary output number proportional to its size.
The resolution is the size of a single step
on the staircase characteristic. `D(output) = V_input/V_ref * 2^n`
* mbed has a single 16-bit ADC, with multiplexer. Its voltage reference is the supply voltage, 3.3 V.
* `AnalogIn`
* read
```c++
#include "mbed.h"
Serial pc( USBTX, USBRX ); //Enable serial port which links to USB
AnalogIn Ain(A0);
float ADCdata;
int main(){
while(1){
ADCdata = Ain; //send an opening text message
wait(0.5);
pc.printf("%1.3f\r\n", ADCdata);
//send data to the terminal
}
}
```
AnalogIn with FFT analysis
K64F will read an analog signal and convert it back to analog signal again to an output pin. We will observe the output pin with both picoscope and a LED.
```c++
#include "mbed.h"
Serial pc( USBTX, USBRX );
AnalogOut Aout(DAC0_OUT);
AnalogIn Ain(A0);
float ADCdata;
int main(){
while(1){
ADCdata = Ain;
Aout = Ain;
wait(1./128);
//sampling rate 128
pc.printf("%1.3f\r\n", ADCdata);
}
}
```
Measure Conversion timing
```c++
#include "mbed.h"
AnalogOut Aout(DAC0_OUT);
AnalogIn Ain(A0);
float ADCdata;
int main(){
while(1){
ADCdata = Ain;
Aout = ADCdata;
wait(2);
//sampling freq = 1/2 hz
//If the sampling frequency is less than the twice of signal frequecy (The signal frequency is 1 Hz), the signal may not be sampled completely
}
}
```
## <a name="mbed04">mbed04-Liquid Crystal Displays </a>
### MicroController behind the LCD – HD44780
* contains an 80-byte RAM (Random Access Memory) to hold the display data, and a ROM (Read Only Memory) for generating the characters.
* controlled by an 8-bit data bus, 3 control lines, and an enable/strobe line (E)
* RS -> register select 0 for instruction register/ 1 for data register
* R/|W -> read(1) or write(0)
* E
* DB0 - DB3, DB4 - DB7
## PC1602F LCD display
* we will use the LCD in 4-bit mode. This means that only the upper 4 bits of the data bus (DB4-DB7) are connected.
* every time a nibble (a 4-bit word) is sent, the E line must be pulsed or toggled on and off.
* E bit must be pulsed for every nibble of display data sent.
## LCD QC1602A with HD44780 interface
### Build your own LCD library
* `main.cpp`:
```c++
#include "LCD.h"
int main()
{
LCD_init(); // call the initialise function
display_to_LCD(0x48); // ‘H’
display_to_LCD(0x45); // ‘E’
display_to_LCD(0x4C); // ‘L’
display_to_LCD(0x4C); // ‘L’
display_to_LCD(0x4F); // ‘O’
for(char x=0x30;x<=0x39;x++)
{
display_to_LCD(x); // display numbers 0-9
}
}
```
* `LCD.h`: declare
```c++
#ifndef LCD_H
#define LCD_H
#include "mbed.h"
void toggle_enable(void); //function to toggle/pulse the enable bit
void LCD_init(void); //function to initialise the LCD
void display_to_LCD(char value); //function to display characters
void set_location(char location);//function to set display location
#endif
```
* `LCD.cpp`: init, sending data funcs for LCD
```c++
#include "LCD.h"
DigitalOut RS(D2); //check out these pin numbers CAREFULLY!!!
DigitalOut E(D3);
BusOut data(D4,D5,D6,D7);
void toggle_enable(void)
{
E=1;
wait(0.001);
E=0;
wait(0.001);
}
//initialise LCD function
void LCD_init(void)
{
wait(0.02); // pause for 20 ms
RS=0; // set low to write control data
E=0; // set low
//function mode
data=0x2; // 4 bit mode (data packet 1, DB4-DB7)
toggle_enable();
data=0x8; // 2-line, 7 dot char (data packet 2, DB0-DB3)
toggle_enable();
//display mode
data=0x0; // 4 bit mode (data packet 1, DB4-DB7)
toggle_enable();
data=0xF; // display on, cursor on, blink on
toggle_enable();
//clear display
data=0x0;
toggle_enable();
data=0x1; // clear
toggle_enable();
}
//display function
void display_to_LCD(char value)
{
RS=1; // set high to write character data
data=value>>4; // value shifted right 4 = upper nibble
toggle_enable();
data=value; // value bitmask with 0x0F = lower nibble
toggle_enable();
}
void set_location(char location)
{
RS=0;
data=(location|0x80)>>4; // upper nibble
toggle_enable();
data=location&0x0F; // lower nibble
toggle_enable();
}
```
### "textLCD" library
`TextLCD lcd(int rs, int e, int d0, int d1, int d2, int d3);`
```c++
#include "mbed.h"
#include "TextLCD.h"
DigitalOut led(LED_RED);
TextLCD lcd(D2, D3, D4, D5, D6, D7);
int main()
{
int x=0;
lcd.printf("Hello World!\n");
while(true)
{
led = !led; // toggle led
lcd.locate(5,1);
lcd.printf("%5i",x); //conuter display
wait(1);
x++;
}
}
```
## <a name="mbed05">mbed05-Interrupts, timers and tasks </a>
//
\ No newline at end of file
Clone repository
  • Home