Ley de Ohm

Fork of FastPWM by Erik -

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers FastPWM_KLXX_K20D50M.cpp Source File

FastPWM_KLXX_K20D50M.cpp

00001 #if defined(TARGET_KLXX) || defined(TARGET_K20D50M)
00002 
00003 #include "FastPWM.h"
00004 
00005 void FastPWM::initFastPWM( void ) {
00006     bits = 16;
00007 }
00008 
00009 void FastPWM::pulsewidth_ticks( uint32_t ticks ) {
00010     *(_pwm.CnV) = ticks;
00011 }
00012 
00013 void FastPWM::period_ticks( uint32_t ticks ) {
00014     *(_pwm.MOD) = ticks - 1;
00015 }
00016 
00017 uint32_t FastPWM::getPeriod( void ) {
00018     return *(_pwm.MOD) + 1;
00019 }
00020 
00021 uint32_t FastPWM::setPrescaler(uint32_t reqScale) {
00022         
00023     //Yes this is ugly, yes I should feel bad about it
00024     volatile uint32_t *TPM_SC = _pwm.MOD - 2;
00025     
00026     const char prescalers[] = {1, 2, 4, 8, 16, 32, 64, 128};
00027     
00028     //If prescaler is 0, return current one
00029     if (reqScale == 0)
00030         return (prescalers[(*TPM_SC) & 0x07]);
00031     
00032     uint32_t retval = 0;
00033     char bin;
00034     
00035     for (bin = 0; bin<8; bin++) {
00036         retval = prescalers[bin];
00037         if (retval >= reqScale)
00038             break;
00039     }
00040     if (bin == 8)
00041         bin = 7;
00042     
00043     //Clear lower 5 bits, write new value:
00044     char clockbits = *TPM_SC & (3<<3);
00045     
00046     //For some reason clearing them takes some effort
00047     while ((*TPM_SC & 0x1F) != 0)
00048         *TPM_SC &= ~0x1F;
00049         
00050     
00051     *TPM_SC = bin + clockbits;
00052     
00053     return retval;   
00054 }
00055 #endif