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
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2019 ARM Limited 00003 * SPDX-License-Identifier: Apache-2.0 00004 */ 00005 00006 #include "mbed.h" 00007 #include "platform/mbed_thread.h" 00008 00009 #define adresse 0x80 00010 #define EXTCLK 1<<6 00011 #define SLEEP 1<<4 00012 #define AI 1<<5 00013 #define PRE_SCALE 0xFE 00014 00015 #define MODE1 0x0 00016 #define MODE2 0x1 00017 #define LED0_ON_L 0x06 00018 00019 I2C motorino(D14,D15); 00020 AnalogIn poti(PA_0); 00021 00022 // Blinking rate in milliseconds 00023 #define BLINKING_RATE_MS 50 00024 00025 char getLED_ON_L(int nr) 00026 { 00027 return LED0_ON_L+4*nr; 00028 } 00029 00030 char getLED_ON_H(int nr) 00031 { 00032 return LED0_ON_L+4*nr+1; 00033 } 00034 00035 char getLED_OFF_L(int nr) 00036 { 00037 return LED0_ON_L+4*nr+2; 00038 } 00039 00040 char getLED_OFF_H(int nr) 00041 { 00042 return LED0_ON_L+4*nr+3; 00043 } 00044 00045 void WriteToASpecificRegister(char RegAdr,char Wert) 00046 { 00047 char daten[2]; 00048 daten[0]=RegAdr; 00049 daten[1]=Wert; 00050 motorino.write(adresse,daten, 2, 0); 00051 } 00052 00053 void setDutyCycle(float dtc, int lednr) 00054 { 00055 unsigned int on,off; 00056 unsigned char onL,offL,onH,offH; 00057 on=0; 00058 off=4095*dtc/100; 00059 onL=on&0xFF; 00060 onH=on>>8&0xf; 00061 offL=off&0xFF; 00062 offH=off>>8&0xf; 00063 WriteToASpecificRegister(getLED_ON_L(lednr),onL); 00064 WriteToASpecificRegister(getLED_ON_H(lednr),onH); 00065 WriteToASpecificRegister(getLED_OFF_L(lednr),offL); 00066 WriteToASpecificRegister(getLED_OFF_H(lednr),offH); 00067 } 00068 00069 void init() 00070 { 00071 WriteToASpecificRegister(MODE1,SLEEP); 00072 WriteToASpecificRegister(PRE_SCALE,121); //50Hz Wert=Runden(25MHz/4096/50)-1 00073 WriteToASpecificRegister(MODE1,EXTCLK); //Switch to external 25MHz Clock 00074 00075 setDutyCycle(25,0); 00076 00077 } 00078 00079 void servo(float pos,int nr,float start=5, float end=10) //pos 0..180° 00080 { 00081 setDutyCycle(start+(end-start)*pos/180.0,nr); 00082 } 00083 00084 int main() 00085 { 00086 int dtc=0; 00087 // Initialise the digital pin LED1 as an output 00088 DigitalOut led(LED1); 00089 init(); 00090 while (true) { 00091 led = !led; 00092 dtc+=1; 00093 if (dtc>180) dtc=0; 00094 servo(poti*180,0); 00095 thread_sleep_for(BLINKING_RATE_MS); 00096 } 00097 }
Generated on Sun Jun 4 2023 16:16:26 by
1.7.2