кароче, чтоб отвязаться
(«Телесистемы»: Конференция «Микроконтроллеры и их применение»)

миниатюрный аудио-видеорекордер mAVR

Отправлено DSN 17 марта 2005 г. 23:27
В ответ на: Люди!!! помогите! как получить пилу с 16f873a цап с SPI отправлено www877A 17 марта 2005 г. 22:53

вот синус двухканальный для MSP430 на Си
изучай разбирайся, дарю.
Делал в карзину, чтоб камень пощупать.
Код переделан из какого-то примера техасовского.


/***********************************************************************/
/* pwm149.c 2000-05-11 */
/* */
/* Demo to Test Timer_B PWM Functions */
/* */
/* This simple program demonstrates the PWM Generation including dead */
/* band Generation with Timer_B on pin P4.1 and P4.2. */
/* The duty cycle will be changed after every full PWM period. When */
/* a lowpass filter is connected, a triangular signal can be seen. */
/* */
/* Following Timer_B registers have been used: */
/* TBCNT: up/down mode, 2x3 groups of TBCCRx */
/* TBCCRO: PWM period */
/* TBCCR1: duty cycle for PWM at P4.1 */
/* TBCCR2: duty cycle for PWM at P4.2 */
/* */
/* Texas Instruments Incorporated */
/***********************************************************************/
#include // Standard Equations
#include

#define inpfreq 8000000 // частота внешнего генератора Гц, вход 1.

/***********************************************************************/
/* Makro definition */
/***********************************************************************/
#define pi __PI

/* --- void WD_Timer( void ) -----------------------------------------*/
#define WD_Timer() \
WDTCTL=0x5A18; /* initialize WD as Timer */ \
IE1|=0x01; /* enable WDT interrupt */ \
WDTCTL = WDTPW + WDTHOLD;

/***********************************************************************/
/* global Variable definition */
/***********************************************************************/
//unsigned int PWM_period;
//unsigned int PWM12_duty;
//unsigned char PWM_status;

unsigned int frqpwm=100000; //частота генерации ШИМ Гц.
unsigned int frqsin=25; //частота генерации SIN Гц.

unsigned char msin[128]; //таблица SIN
unsigned int stepi; //шаг.
unsigned int idxi=0; //индекс выборки синуса.
unsigned int gaini; //Масштаб синуса.
/***********************************************************************/
/* Function declaration */
/***********************************************************************/
void PWM12_init(void);
void PWM12_update( void );
unsigned int sinx(unsigned int tck);

/***********************************************************************/
/* PWM12_init */
/* initializes Timer_B in up/down mode and select 2x3 groups of CCR to */
/* generate two PWM signals with CCR1 and CCR2. CCR0 defines period. */
/* Timer_B overflow (TBR goes from CCR0 to 0) will update PWM */
/***********************************************************************/
//режимы таймера B.
#define capmode 0x0 //disable
#define cntrmode 0x1 // Up Mode
#define selinp 0x1 // Select ACLK
#define inpdiv 0x0 // Prescaller = /1
#define outmode 0x3 // OutMode Set/Reset
#define eint 0x1 // Interrupt Enable
#define tmrclr 0x1 // Timer Clear
#define sizetmr 0x0 // 16 bit timer
#define loadgr 0x0 // Load Group
//**********************************************************
void PWM12_init(void)
{
TBCCR0=inpfreq/frqpwm; /* load period register */
TBCCR1=0xffff; //duty; /* load duty cycle register CCR1 */
TBCCR2=0xffff; //duty+PWM_dead; /* load duty cycle register CCR2 + dead band */
TBCCTL0=0x0000;

#define tmrout0 0x00 // out=0
#define tmrout1 0x04 // out=1

#define tmrienx 0x10 // Inerrup Enable
#define tmridsx 0x00 // Inerrup Disable

#define tmoutmd0 0x00*0x20 // OutPut Mode Select
#define tmoutmd1 0x01*0x20
#define tmoutmd2 0x02*0x20
#define tmoutmd3 0x03*0x20
#define tmoutmd4 0x04*0x20
#define tmoutmd5 0x05*0x20
#define tmoutmd6 0x06*0x20
#define tmoutmd7 0x07*0x20

#define tmcaptmd 0x100 //capture mode
#define tmcompmd 0x0 //compare mode

#define tmasycap 0x0 // asynchronous capture
#define tmsyncap 0x800 // synchronous capture

#define tmcapina 0*0x1000 // Input CCIxA is selected
#define tmcapinb 1*0x1000 // Input CCIxB is selected
#define tmcaping 2*0x1000 // GND
#define tmcapinv 3*0x1000 // VCC

#define tmcapmds 0*0x4000 //Disabled The capture mode is disabled.
#define tmcapmpo 1*0x4000 //Pos. Edge Capture is done with rising edge.
#define tmcapmne 2*0x4000 //Neg. Edge Capture is done with falling edge.
#define tmcapmbo 3*0x4000 //Both Edges Capture is done with both rising and falling edges.
TBCCTL1= tmcapmds|tmcaping|tmasycap|tmcompmd|tmoutmd3|tmridsx|tmrout0;
TBCCTL2= tmcapmds|tmcaping|tmasycap|tmcompmd|tmoutmd3|tmridsx|tmrout0;
TBCTL= loadgr*0x2000 +sizetmr*0x800 +selinp*0x100 +inpdiv*0x40 +cntrmode*0x10 +tmrclr*0x4 +eint*0x2; //
}
/***********************************************************************/
void inithard(void) //инициализация железяк(ядра).
{
//DCOCTL = 0;
#define divaclk1 0x00
#define divaclk2 0x10
#define divaclk4 0x20
#define divaclk8 0x30
#define xt1lo 0x00
#define xt1hi 0x40
#define xt2off 0x80
#define xt2on 0x00
BCSCTL1 = xt2off | xt1hi | divaclk1 | 0x07;
#define slminclk 0x00
#define slminclk1 0x40
#define clmexclk1 0xC0
#define clmexclk2 0x80
#define mdiv1 0x00
#define mdiv2 0x10
#define mdiv4 0x20
#define mdiv8 0x30
#define selsin 0x00
#define selsex 0x08
#define divsmclk1 0x00
#define divsmclk2 0x02
#define divsmclk4 0x04
#define divsmclk8 0x06
BCSCTL2 = clmexclk1 | mdiv1 | selsin | divsmclk1 ;
}
/***********************************************************************/
void initvar(void) //(ре)инициализация переменых ШИМа.
{
volatile unsigned long divi;
divi = ((unsigned long)frqsin*128*256)/(unsigned long)frqpwm;
stepi=(unsigned int)divi;
gaini=inpfreq/frqpwm;
TBCCR0=(inpfreq/frqpwm);
}
/***********************************************************************/
void initsin(void) //расчет таблици полуволны синуса.
{
unsigned char i;
for(i=0;i<128;i++){
msin[i]=(unsigned char)(255-sin(pi*i/128)*255);
}
}
/***********************************************************************/
/* main */
/***********************************************************************/
void main( void ) //Основной цикл.
{
WD_Timer();

//P1DIR|=0x01; /* P1.0 is test output for LED */
P4SEL|=0x06; /* select P4.1 and P4.2 for PWM output */
P4DIR|=0x0f; /* select P4.1 and P4.2 for output */

inithard();
initsin();
initvar();
PWM12_init();
_EINT(); /* enable global interrupts */

while(1)
{
P4OUT = P4OUT ^ 0x01;
}
//LPM0; /* switch CPU off */

} /* end of main */
/***********************************************************************/
/* Watchdog interrupt service routine */
/***********************************************************************/
interrupt [WDT_VECTOR] void WD( void )
{

}
/***********************************************************************/
/* Timer_B7 Interrupt Vector (TBIV) handler */
/***********************************************************************/
interrupt [TIMERB1_VECTOR] void Timer_B(void)
{
switch( TBIV )
{
case 2: break; /* CCR1 */
case 4: break; /* CCR2 */
case 6: break; /* CCR3 */
case 8: break; /* CCR4 */
case 10: break; /* CCR5 */
case 12: break; /* CCR6 */
case 14: PWM12_update(); /* Timer_B7 overflow */
break;
}
TBCTL = TBCTL & 0xfffe;
}
/***********************************************************************/
/* Timer_B7 Timer Overflow Interrupt Service Routine */
/* updates PWM_duty cycle register CCR1 and CCR2 */
/***********************************************************************/
void PWM12_update( void ) //обновление ШИМ.
{
volatile unsigned char idx;
volatile unsigned int divp;
volatile unsigned long dvp;
volatile long dvpr;
idxi=0xffff&(idxi+stepi); //изменение индекса выборки
idx=0xff&(idxi>>8); //коррекция индекса для выборки из таблицы.
dvp=(unsigned long)msin[idx&0x7f]*(unsigned long)(gaini); //масштабирование получнного значения.
dvpr=(long)(msin[(idx+1)&0x7f]-msin[idx&0x7f])*(long)(idxi&0xff);
divp=((dvp+dvpr)>>8); //коррекция полученного значения.
if((int)divp<0){divp=0;}
if(idx&0x80){TBCCR1=divp;TBCCR2=0xffff;} //выбор полуволны и уст.
else{TBCCR2=divp;TBCCR1=0xffff;}
P4OUT = P4OUT ^ 0x08;
}
/***********************************************************************/


Составить ответ  |||  Конференция  |||  Архив

Ответы



Перейти к списку ответов  |||  Конференция  |||  Архив  |||  Главная страница  |||  Содержание  |||  Без кадра

E-mail: info@telesys.ru