ANDROID cc254x RGB多彩LED程序
[复制链接]
CC254X程序
// cc2541
// -----------------
// /|\| XIN|-
// | | |
// --|RST XOUT|-
// | |
// | P0.4/T1.2|--> RED
// | P0.5/T1.3|--> GREEN
// | P0.6/T1.4|--> BLUE
#include <hal_types.h>
#include "ioCC254x_bitdef.h"
#include "ioCC2541.h"
void pwmEnable(void)
{
T1CCTL2 = (T1CCTL2 & ~T1CCTLn_CMP) | T1CCTLn_MODE | T1CCTLn_CMP_SET_CMP_UP_CLR_0;//set channel 2
T1CCTL3 = (T1CCTL3 & ~T1CCTLn_CMP) | T1CCTLn_MODE | T1CCTLn_CMP_SET_CMP_UP_CLR_0;//and 3
T1CCTL4 = (T1CCTL4 & ~T1CCTLn_CMP) | T1CCTLn_MODE | T1CCTLn_CMP_SET_CMP_UP_CLR_0;//and 4
T1CTL = (T1CTL & ~(T1CTL_MODE | T1CTL_DIV)) | T1CTL_MODE_MODULO | T1CTL_DIV_1;//start timer1
}
void pwmDisable(void)
{
T1CCTL2 = (T1CCTL2 & ~T1CCTLn_CMP);//disable channel 2
T1CCTL3 = (T1CCTL3 & ~T1CCTLn_CMP);//and 3
T1CCTL4 = (T1CCTL4 & ~T1CCTLn_CMP);//and 4
T1CTL = (T1CTL & ~(T1CTL_MODE | T1CTL_DIV));//stop timer1
}
void pwmInit(void)
{
P0SEL |= BIT4+BIT5+BIT6; // Selects P1_0 as peripheral I/O.
P0DIR |= BIT4+BIT5+BIT6; // and output.
// PERCFG &= ~PERCFG_T1CFG; // 0 = alt.1 1=alt.2
// P2SEL |= P2SEL_PRI0P1; // Port1 only When set Timer 1 has priotity over USART 0.
P2DIR |= P2DIR_PRIP0_T1_2_3; // Timer 1 channels 2-3 has priority, then USART 1, then USART 0, then Timer 1 channels 0-1.
pwmDisable();
}
void pwmSetValue(uint8 *pValue)
{
pwmDisable();
T1CC0H = pValue[0];
T1CC0L = pValue[1]; //0 总频率
T1CC2H = T1CC0H - pValue[2];
T1CC2L = T1CC0L - pValue[3]; //channel 2占空比
T1CC3H = T1CC0H - pValue[4];
T1CC3L = T1CC0L - pValue[5]; //channel 3占空比
T1CC4H = T1CC0H - pValue[6];
T1CC4L = T1CC0L - pValue[7]; //channel 4占空比
pwmEnable();
} 复制代码
/*********************************************************************
* @fn simpleProfileChangeCB
*
* @brief Callback from SimpleBLEProfile indicating a value change
*
* @param paramID - parameter ID of the value that was changed.
*
* @return none
*/
static void simpleProfileChangeCB( uint8 paramID )
{
uint8 newValue[10];
switch( paramID )
{
case SIMPLEPROFILE_CHAR1:
SimpleProfile_GetParameter( SIMPLEPROFILE_CHAR1, &newValue );
pwmSetValue(newValue);
#if (defined HAL_LCD) && (HAL_LCD == TRUE)
HalLcdWriteString((char*) newValue, HAL_LCD_LINE_3 );
// HalLcdWriteStringValue( "Char 1:", (uint16)(newValue[0]), 10, HAL_LCD_LINE_3 );
#endif // (defined HAL_LCD) && (HAL_LCD == TRUE)
break;
case SIMPLEPROFILE_CHAR3:
SimpleProfile_GetParameter( SIMPLEPROFILE_CHAR3, &newValue );
#if (defined HAL_LCD) && (HAL_LCD == TRUE)
HalLcdWriteStringValue( "Char 3:", (uint16)(newValue), 10, HAL_LCD_LINE_3 );
#endif // (defined HAL_LCD) && (HAL_LCD == TRUE)
break;
case SIMPLEPROFILE_CHAR5:
SimpleProfile_GetParameter( SIMPLEPROFILE_CHAR5, &newValue );
#if (defined HAL_LCD) && (HAL_LCD == TRUE)
HalLcdWriteStringValue( "Char 5:", (uint16)(newValue[0]), 10, HAL_LCD_LINE_3 );
#endif // (defined HAL_LCD) && (HAL_LCD == TRUE)
break;
case SIMPLEPROFILE_CHAR6:
SimpleProfile_GetParameter( SIMPLEPROFILE_CHAR6, &newValue );
#if (defined HAL_LCD) && (HAL_LCD == TRUE)
HalLcdWriteStringValue( "Char 6:", (uint16)(newValue[0]), 10, HAL_LCD_LINE_3 );
#endif // (defined HAL_LCD) && (HAL_LCD == TRUE)
break;
default:
// should not reach here!
break;
}
}
复制代码 安卓程序
void setValue(int fqc,int r,int g,int b)
{
if (rgbCharacteristic != null) {
buffer[0] = (byte)(fqc>>8 & 0xff);
buffer[1] = (byte)(fqc & 0xff);
buffer[2] = (byte)(r>>8 & 0xff);
buffer[3] = (byte)(r & 0xff);
buffer[4] = (byte)(g>>8 & 0xff);
buffer[5] = (byte)(g & 0xff);
buffer[6] = (byte)(b>>8 & 0xff);
buffer[7] = (byte)(b & 0xff)
rgbCharacteristic.setValue(buffer);
mBluetoothLeService.writeCharacteristic(rgbCharacteristic);
}
} 复制代码 seekBar1 = (SeekBar)findViewById(R.id.seekBar1);
seekBar2 = (SeekBar)findViewById(R.id.seekBar2);
seekBar3 = (SeekBar)findViewById(R.id.seekBar3);
seekBar1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
/**
* 拖动条停止拖动的时候调用
*/
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
//setValue(fqc,r,g,b);
}
/**
* 拖动条开始拖动的时候调用
*/
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
/**
* 拖动条进度改变的时候调用
*/
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
r =(int) (fqc/100.00*progress);
setValue(fqc,r,g,b);
// tv.setText(String.format("R:%d G:%d B:%d %s",r,g,b,""));
}
});
seekBar2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
/**
* 拖动条停止拖动的时候调用
*/
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
//setValue(fqc,r,g,b);
}
/**
* 拖动条开始拖动的时候调用
*/
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
/**
* 拖动条进度改变的时候调用
*/
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
g =(int) (fqc/100.00*progress);
setValue(fqc,r,g,b);
// tv.setText(String.format("R:%d G:%d B:%d %s",r,g,b,""));
}
});
seekBar3.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
/**
* 拖动条停止拖动的时候调用
*/
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
//setValue(fqc,r,g,b);
}
/**
* 拖动条开始拖动的时候调用
*/
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
/**
* 拖动条进度改变的时候调用
*/
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
b =(int) (fqc/100.00*progress);
setValue(fqc,r,g,b);
// tv.setText(String.format("R:%d G:%d B:%d %s",r,g,b,""));
}
});
} 复制代码
代码:
SimpleBLEPeripheral RGB LED.rar
(4.9 MB, 下载次数: 118)
ANDROID RGB LED V1.1.rar
(1.04 MB, 下载次数: 88)