1. Select the actuator of the solar panel that can track the sun. 375516[/attach] Reduction ratio (Reduction ratio=Input speed÷Output speed=Number of teeth of driven gear÷Number of teeth of driving gear) Number of pulses: 64 Step angle: 5.625 degrees (64 pulses per circle) Function: Determine the angular displacement of rotation by the number of pulses input, generally to control the offset angle of the wheels of the trolley, etc. Driving mode: 4 phases and 8 beats (forward A-AB-B-BC-C-CD-D-DA-A......) The phase excitation sequence of the stepping motor is: 375517 [/attach] 375518 [/attach] 375520[/attach] A stepper motor is an actuator that converts electrical pulses into angular displacement. In simple terms: when the stepper driver receives a pulse signal, it drives the stepper motor to rotate a fixed angle (and step angle) in the set direction. You can control the angular displacement by controlling the number of pulses to achieve the purpose of accurate positioning; at the same time, you can control the speed and acceleration of the motor by controlling the pulse frequency to achieve the purpose of speed regulation. Stepper Motor28BYJ48 four-phase eight-beat motor, voltage is DC5V-DC12V. When a series of continuous control pulses are applied to the stepper motor, it can rotate continuously. Each pulse signal corresponds to a change in the power-on state of one or two phases of the stepper motor winding, which corresponds to the rotor rotating a certain angle (a step angle). When the change in the power-on state completes a cycle, the rotor rotates one tooth pitch. Four-phase stepper motors can operate in different power-on modes. Common power-on modes include single (single-phase winding power-on) four beats (ABCDA...), double (two-phase winding power-on) four beats (AB-BC-CD-DA-AB-...), and eight beats (A-AB-B-BC-C-CD-D-DA-A...) 2. Start to install solar panels on stepper motors
3. Install the light intensity sensor module. The light intensity sensor module I chose is the BH1750 module. The BH1750 is an IIC working device. I use it to collect light intensity and feed the data back to the stepper motor to adjust the direction of the solar panel to track the direction with high light intensity. 4. Modular program Because the system I built is relatively large, in order to make the program clear and organized, I use modular programming. The following is the stepper motor program: #ifndef __STEPMOTOR_H #define __STEPMKOTOR_H #include
void Stepmotor_Angle(unsigned int angle, unsigned char dir,unsigned char speed); void Stepmotor_Stop(void); void Stepmotor_Delayms(unsigned int x); #endif #include "stepmotor.h" code const unsigned char CCW[8] = {0x08 ,0x0c,0x04,0x06,0x02,0x03,0x01,0x09};//Clockwise rotation code const unsigned char CW[8] = {0x09,0x01,0x03,0x02,0x06,0x04,0x0c,0x08};// Rotate counterclockwise void Stepmotor_Delayms(unsigned int x) { unsigned char y; for( ; x > 0; x--) { for(y = 110; y > 0; y--); } } /**************************************** ******************************* *Function name: Step_Motor *Function parameters: dir: Direction: dir = 0 clockwise Rotate, dir = 1 counterclockwise rotation speed: 4: fastest 10: slowest other speeds are selected between ?~10 * Function: The stepper motor rotates in a certain direction * Return value: None * Function Author: bqgup *Completion date: 2017.9.12 **************************************** ********************************/ static void Step_Motor(unsigned char dir,unsigned char speed) { unsigned char i, j; for(j = 0; j < 8; j++) { for(i = 0; i < 8; i++) { if(dir == 0) { P1 = CCW; } else if(dir == 1) { P1 = CW; } Stepmotor_Delayms(speed);//slowest 7 gears } } } /********************************* ****************************************** *Function name; Stepmotor_Angle *Function parameters : angle: angle dir: direction dir = 0 clockwise, dir = 1 counterclockwise speed: rotation speed, increase from 4 upwards, the larger the value, the slower the speed, please note that it cannot exceed the specified size of the data type *Function function : The stepper motor rotates a certain angle in a certain direction *Return value: None *Function author: bqgup *Completion date: 2017.9.12 ********************* ************************************************** */ void Stepmotor_Angle(unsigned int angle, unsigned char dir,unsigned char speed) { unsigned int pulse; unsigned int i; pulse = angle / 11.25 + 0.5; for(i = pulse; i > 0; i--) { Step_Motor(dir,speed); } } void Stepmotor_Stop(void) { P1 = 0x00; } [/code] The following is the BH1750 light intensity module acquisition program[ #ifndef BH1750_H #define BH1750_H #include
#include
#define SlaveAddress 0x46 //Define the slave address of the device in the IIC bus, modify it according to the different ALT ADDRESS address pins //When the ALT ADDRESS pin is grounded, the address is 0xA6, and when it is connected to the power supply, the address is 0x3A sbit SCL=P1^2; //IIC clock pin definition sbit SDA=P1^3; //IIC data pin definition/********************************************************************** *Function name: delay_nms *Function function: delay xms *Function parameter: x *Return value: None*******************************************************************/ void delay_nms(unsigned int x); /************************************************************************** *Function name: BH1750_Start *Function function: I2C bus start signal *Function parameter: None *Return value: None***********************************************************************/ void BH1750_Start(); /************************************************************************** *Function name: BH1750_Stop *Function function: I2c bus termination signal *Function parameter: None *Return value: None********************************************************************/ void BH1750_Stop(); /************************************************************************** *Function name: BH1750_SendACK *Function function: I2c bus sends response signal *Function parameter: ack *Return value: None***********************************************************************/ void BH1750_SendACK(bit ack); /************************************************************************** *Function name: BH1750_RecvACK *Function function: I2c bus receives response signal *Function parameter: None *Return value: CY ***************************************************************************/ bit BH1750_RecvACK(); /********************************************************************** *Function name: BH1750_SendByte *Function function: I2C bus writes a byte *Function parameter: None *Return value: None*******************************************************************/ void BH1750_SendByte(unsigned char dat); /************************************************************************** *Function name: BH1750_RecvByte *Function function: I2C bus reads a byte *Function parameter: None *Return value: dat ***************************************************************************/ unsigned char BH1750_RecvByte(); /************************************************************************** *Function name: Single_Write_BH1750 *Function function: BH1750 single byte write *Function parameter: None *Return value: None****************************************************************/ void Single_Write_BH1750(unsigned char REG_Address); /********************************************************************** *Function name: Single_Read_BH1750 *Function function: BH1750 single byte read *Function parameter: None *Return value: None*******************************************************************/ //unsigned char Single_Read_BH1750(uchar REG_Address); /************************************************************************** *Function name: Init_BH1750 *Function function: BH1750 initialization *Function parameter: None *Return value: None********************************************************************/ void Init_BH1750(); #endif #include "bh1750.h" /********************************************************************** *Function name: delay_nms *Function function: delay xms *Function parameter: x *Return value:None********************************************************************/ void delay_nms(unsigned int x) { unsigned int y; for(; x > 0; x--) { for(y = 110; y > 0; y--); } } /************************************************************************** *Function name: Delay5us *Function function: delay 5us *Function parameter: None *Return value: None***********************************************************************/ void Delay5us() { _nop_();_nop_();_nop_();_nop_();_nop_(); _nop_();_nop_();_nop_();_nop_(); _nop_();_nop_();_nop_(); _nop_();_nop_();_nop_();_nop_(); } /********************************************************************** *Function name: BH1750_Start *Function function: I2c bus start signal *Function parameters: None *Return value: None **********************************************************************/ void BH1750_Start() { SDA = 1; //Pull up the data line SCL = 1; //Pull up the clock line Delay5us(); //Delay SDA = 0; //Generate a falling edge Delay5us(); //Delay SCL = 0; //Pull down the clock line } /************************************************************************** *Function name: BH1750_Stop *Function function: I2c bus termination signal *Function parameters: None *Return value: None *************************************************************************/ void BH1750_Stop() { SDA = 0; //Pull down the data line SCL = 1; //Pull up the clock line Delay5us(); //Delay SDA = 1; //Generate a rising edge Delay5us(); //Delay} /********************************************************************** *Function name: BH1750_SendACK *Function function: I2c bus sends a response signal *Function parameter: ack *Return value: None***********************************************************************/ void BH1750_SendACK(bit ack) { SDA = ack; //Write response signal SCL = 1; //Pull up the clock line Delay5us(); //Delay SCL = 0; //Pull down the clock line Delay5us(); //Delay} /************************************************************************** *Function name: BH1750_RecvACK *Function function: I2c bus receives response signal *Function parameters: None *Return value: CY **********************************************************************/ bit BH1750_RecvACK() { SCL = 1; //Pull up the clock line Delay5us(); //Delay CY = SDA; //Read response signal SCL = 0; //Pull down the clock line Delay5us(); //Delay return CY; } /************************************************************************** *Function name: BH1750_SendByte *Function function: I2c bus writes a byte *Function parameters: None *Return value:None********************************************************************/ void BH1750_SendByte(unsigned char dat) { unsigned char i; for (i=0; i<8; i++) //8-bit counter{ dat <<= 1; //Shift out the highest bit of the data SDA = CY; //Send data port SCL = 1; //Pull up the clock line Delay5us(); //Delay SCL = 0; //Pull down the clock line Delay5us(); //Delay} BH1750_RecvACK(); } /****************************************************************************** *Function name: BH1750_RecvByte *Function function: I2C bus reads a byte *Function parameter: None *Return value: dat ***********************************************************************/ unsigned char BH1750_RecvByte() { unsigned char i; unsigned char dat = 0; SDA = 1; //Enable internal pull-up, prepare to read data, for (i=0; i<8; i++) //8-bit counter{ dat <<= 1; SCL = 1; //Pull up the clock line Delay5us(); //Delay dat |= SDA; //Read data SCL = 0; //Pull down the clock line Delay5us(); //Delay } return dat; } /****************************************************************************** *Function name: Single_Write_BH1750 *Function function: BH1750 single byte write *Function parameter: None *Return value: None*******************************************************************/ void Single_Write_BH1750(unsigned char REG_Address) { BH1750_Start(); //Start signal BH1750_SendByte(SlaveAddress); //Send device address + write signal BH1750_SendByte(REG_Address); //Internal register address, please refer to Chinese pdf page 22//BH1750_SendByte(REG_data); //Internal register data, please refer to Chinese pdf page 22 BH1750_Stop(); //Send stop signal } /****************************************************************************** *Function name: Single_Read_BH1750 *Function function: BH1750 single byte read *Function parameter: None *Return value: None *************************************************************************/ /* unsigned char Single_Read_BH1750(uchar REG_Address) { unsigned char REG_data; BH1750_Start(); //Start signal BH1750_SendByte(SlaveAddress); //Send device address + write signal BH1750_SendByte(REG_Address); //Send storage unit address, starting from 0 BH1750_Start(); //Start signal BH1750_SendByte(SlaveAddress+1); //Send device address + read signal REG_data=BH1750_RecvByte(); //Read register data BH1750_SendACK(1); BH1750_Stop(); //Stop signal return REG_data; } */ /************************************************************************** *Function name: Init_BH1750 *Function function: BH1750 initialization *Function parameter: None *Return value:None************************************************************************/ void Init_BH1750() { Single_Write_BH1750(0x01); } void BH1750_DataAlter(unsigned int temp_data) // Convert data into units, tens, hundreds, thousands, and ten thousand { wan=temp_data/10000+0x30 ; temp_data=temp_data%10000; //Remainder operation qian=temp_data/1000+0x30 ; temp_data=temp_data%1000; //Remainder operation bai=temp_data/100+0x30 ; temp_data=temp_data%100; //Remainder operation shi=temp_data/10+0x30 ; temp_data=temp_data%10; //Remainder operation ge=temp_data+0x30; } void Multiple_read_BH1750(void) { unsigned char i; BH1750_Start(); //Start signal BH1750_SendByte(SlaveAddress+1); //Send device address + read signal for (i=0; i<3; i++) //Continuously read 6 address data and store in BUF { BUF = BH1750_RecvByte(); //BUF[0] stores the data in address 0x32if (i == 3) { BH1750_SendACK(1); //The last data needs to return NOACK } else { BH1750_SendACK(0); //Response ACK } } BH1750_Stop(); //Stop signal delay_nms(5); } void Disposal_BH1750() { Single_Write_BH1750(0x01); // power on Single_Write_BH1750(0x10); // H-resolution mode delay_nms(180); // Delay 180ms Multiple_read_BH1750(); // Continuously read data and store in BUF} [/code]
|