⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 samplecerebv2.c

📁 a zipped example of communication between the Cerebellum 16f877 pic board and the CMUcam. There are
💻 C
字号:
// This is Version 2 of cereb //
/* Change made:
Servo mask now enables partial servo usage (and partial digital output usage)
Button1 moved to pin 0 and motor direction moved to pin 1.

*/

#include "cerebv2.h" // load declarations
#include "cerebv2.c" // load useful cerebellum functions

// API provided for cerebv2 //////////////////////
//
// call init_cerebellum() first in your main
// turn on an led with set_bit(PORTB, (GREEN|YELLOW)); turn off with clear_bit
// (PORTB & BTNn) is true if button n is depressed and false otherwise
//
// To output on a digital pin on portD, use set_bit(PORTD, n); where n is 0-7 (and clear_bit)
// These are the pins to generally use for digital output.
// But if you're using the Servoes, you will have to set the mask for the servoes
// appropriately so that you can preserve some of the pins for your own digital out
// purposes.  Read the Servoes section below.
// 
// call ser_init(SER_115200) to set up hardware serial port
// ser_tx(char c) sends a character out over the serial line
// ser_putstring(const char *text) writes a CONST string out the serial line
// ser_writechar(char c) writes out a character in decimal (good for debugging!)
// ser_rcv() is a blocking receive char call that returns a char
//		note that if the input buffer has overflowed it dumps the buffer
//		and continues waiting for a new character to return!
// ser_rcv_nb() is a nonblocking receive char call that returns a char
//          or if there is nothing waiting, returns a 0. Does NOT check for
//		frame error and overflow conditions!!!
// to save memory, if not using the serial port, don't call ser_init() and then
// don't use any of the above functions.
// 
// to use servoes, begin by calling servo_init() in your main.
// then before expecting servoes to get used make servo_state=1;
// servoes use interrupts, so make sure you're starting interrupts:
//		set_bit(INTCON, GIE);
// then command a servo by commanding servo_pos[n]=p. This will drive
// the servo on port Dn to position p.  If p is zero, it deactivates the servo.
// to save memory, if not using servoes don't call servo_init(), do not
// put "set_bit(INTCON, GIE);" in your code, and also
// go to cereb.c and find void interrupt(void) and comment out the line that
// calls do_servo().
//
// to use some servoes and other portD lines for direct digital outputs, note
// the variable servo_mask.  This defaults to 11111111b, which means all pins
// on portD are configured for servo output use.  If you want to have D0,D1
// and D3 be servo pins and the rest controlled directly by you, using set_bit,
// then in Main() you would include the line: "servo_mask = 00000111b;"
// Then the servo controller would ignore pins D3 - D7 and you can set them
// as desired. For example you could set pin3 to be high: "set_bit(PORTD,3);"
// Note the tristate is still configuring ALL of portD for digital output (not input).
//
// to use the analog-digital input lines, first in main you must initialize
// with the command: ADCON1=0;
// to read analog input channel ch, use adc_read(ch) which returns a char
// ch should be 0-7 inclusive. numbering on cerebellum goes from A0 as channel
// zero, left-to-right and top-to-bottom, so that E2 is analog channel 7
//
// to use the motor PWM outputs, first call pwm_init() in your code.
// if both commands are in there, call ser_init() first. The motor command
// is pwm_setvel8(n,dir,duty) where n=0 or 1 (0 means Motor1 on Cerebellum
// and 1 means Motor2 on Cerebellum). dir is direction (0 or 1) and duty is
// a char, with 0 meaning off and 255 meaning full-on.
// To save memory, if you are not using motor PWM then comment out pwm_init() in
// main().
// // 
////////////////////
// 


int postest = 100;

// sample that blinks one LED and blinks the other when a button is held down //
// it also tells you over serial what you type in (1 char at a time) and 
// moves a servo on D1 when the button is held down, one step at a time.
void button_sample(void)
{
 char inc;
 while (1) {
   if (PORTB & BTN2) // if button2 is depressed
   {
	set_bit(PORTB, GREEN); // turn on green led
	inc = ser_rcv_nb(); // non-blocking serial port receive
	ser_putstring("\n\rReceived: ");
	if (inc == 0) {
		ser_putstring("nothing to read...");
	} else {
		ser_tx(inc);
	}
	ser_putstring("\n\rEnd1");
	
	postest--;
	servo_pos[1] = postest; // re-position servo by 1
	inc = adc_read(7);      // read analog input 7 (E2)
	ser_putstring("ADC7 read: ");
	ser_writechar(inc);
	ser_putstring("\n\rEnd2\n\r");	 	
   } // end if button2 depressed
   set_bit(PORTB, YELLOW); // turn on yellow led
   delay_ms( 250 );
   clear_bit(PORTB, YELLOW); // turn off both led's
   clear_bit(PORTB, GREEN);
   delay_ms( 250 );
   
 } // end while

} // end button_sample() //

void main(void)
{
	init_cerebellum(); // configure cerebellum
	ser_init(SER_115200); // start up serial port handling
	ser_putstring("\n\rWelcome to Cerebellum!\n\r");
	servo_init(); // start up servo control
	servo_state = 1;
	set_bit(INTCON, GIE); // start up interrupts- required for servo control
	servo_mask = 11111111b; // set up all D0-D7 pins for servo control

	servo_pos[1] = postest; // take servo 1 to position 100  //

	ADCON1 = 0; // initialize analog-digital converter //

	pwm_init(); // initialize pwm motor output

      button_sample();

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -