Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
- Committer:
- Sainratp
- Date:
- 2017-10-23
- Revision:
- 0:703249a56b9d
- Child:
- 1:5c0ca9bdf810
File content as of revision 0:703249a56b9d:
#include "mbed.h"
#include "DO/DO.h"
#include "MO/MO.h"
#include "rtos.h"
#include "config.h"
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut out(PIN_OUT);
InterruptIn in(PIN_IN);
Serial pc(USBTX,USBRX);
Thread sendCharThread;
Thread receiveBitThread;
Ticker ticker;
Mutex mux;
Timer timer;
Mail<char,16> mailBoxReception;
Mail<char,16> mailBoxEnvoi;
Queue <void,16> queueBitReception;
//MODULATION
void sendChar()
{
while(1) {
osEvent evt = mailBoxEnvoi.get();
if(evt.status == osEventMail) {
char* c = (char*)evt.value.p;
for(int i =0 ; i<8 ; i++) {
if(((*c>>7-i)&0x01)==1) {
out = 1;
Thread::signal_wait(0x01);
out = 0;
Thread::signal_wait(0x01);
} else {
out = 0;
Thread::signal_wait(0x01);
out = 1;
Thread::signal_wait(0x01);
}
}
}
}
}
//DEMODULATION
void receiveChar()
{
char res;
while(1) {
res=0x00;
for(int i =0; i<8; i++) {
osEvent evt = queueBitReception.get();
if(evt.status == osEventMessage) {
res = res | (((char)evt.value.p)<<i);
}
}
mailBoxReception.put((char*)res);
}
}
//DEMODULATION
void receiveBit()
{
int oldTime;
bool bitType=false;
Thread::signal_wait(0x02);
oldTime = timer.read_ms();
while(1) {
Thread::signal_wait(0x01);
if(timer.read_ms()-oldTime<T) {
Thread::signal_wait(0x01);
oldTime=timer.read_ms();
} else {
oldTime=timer.read_ms();
bitType=!bitType;
}
if(bitType) {
queueBitReception.put((void*)0x01);
} else {
queueBitReception.put((void*)0x00);
}
}
}
void interruptRise()
{
receiveBitThread.signal_set(0x01);
}
void interruptFall()
{
receiveBitThread.signal_set(0x01);
}
void wake()
{
sendCharThread.signal_set(0x01);
led3=!led3;
}
int main()
{
timer.start();
ticker.attach(&wake,T/2.0);
in.rise(&interruptRise);
in.fall(&interruptFall);
led3=1;
wait(1);
while(1) {
if(pc.readable()) {
sendCharThread.start(&sendChar);
}
}
}