求高手帮合并下c程序
解决时间 2021-01-29 17:37
- 提问者网友:谁的错
- 2021-01-28 19:19
#include
#include
int main(void)
{
uint16_t count;
uint16_t timeLeft,timeRight;
usta_Init();
while(1)
{
DDRC|=_BV(PC0);
PORTC|=_BV(PC0);
delay_nms(2);
TCNT1=0;
DDRC&=~_BV(PC0);
TCCR1B=_BV(CS10);
while((PINC&0x01)!=0);
count=TCNT1;
timeLeft=count/4;
DDRC|=_BV(PC1);
PORTC|=_BV(PC1);
delay_nms(2);
TCNT1=0;
DDRC&=~_BV(PC1);
TCCR1B=_BV(CS10);
while((PINC&0x02)!=0);
count=TCNT1;
timeRight=count/4;
printf("timeLeft = %u us ",timeLeft);
printf("timeRight = %u us\n",timeRight);
delay_nms(100);
}
}
另
#include
#include
int PC0_state(void)
{
return (PINC&0x01)?1:0;
}
int main(void)
{
usta_Init();
printf("Program Running!");
DDRD|=_BV(PD7);
DDRC|=_BV(PC2)|_BV(PC3);
for(uint16_t counter=1;counter<=600;counter++)//开始/复位信号
{
PORTD|=_BV(PD7);
delay_nus(166);
PORTD&=~_BV(PD7);
delay_nus(166);
}
while(1)
{
if(PC0_state()==0)
{
PORTD|=_BV(PD7);
delay_nus(1000);
PORTD&=~_BV(PD7);
delay_nus(1000);
}
}
}
最佳答案
- 五星知识达人网友:英雄的欲望
- 2021-01-28 19:42
#include
//可去掉#include
using namespace std;
typedef int datatype;
#define maxsize 1024
typedef struct
{
datatype data[maxsize];
int last;
}sequenlist;
sequenlist *combine (sequenlist *scrptr,sequenlist *sinkptr);//添加函数声明
void main ()
{
sequenlist *Aptr,*Bptr,*Cptr;
Aptr = new sequenlist;//注意,要申请空间,初始化指针,下同
Bptr = new sequenlist;
Cptr = NULL;
//Cptr不用申请空间,因为后面有返回指针,这里初始为NULL,以防非法操作
char ch; //注意这里
int i=0;
//注意这里
do
{
ch = getchar();
Aptr->data[i]= ch - '0';//*Aptr.data[i]——>Aptr->data[i]
i++;
}
while (ch!='\n' && i
Aptr->last = i-1; //添加这句
i = 0;
do
{
ch = getchar();
Bptr->data[i]= ch - '0';//*Aptr.data[i]——>Aptr->data[i]
i++;
}
while (ch!='\n' && i
Bptr->last = i-1;
Cptr=combine(Aptr,Bptr);//注意这里
for(i=0;ilast;i++)//注意
printf("%d",Cptr->data[i]);
delete Aptr;//前面new的,现在要delete,否则内存泄漏(虽然编译可通过)
delete Bptr;
Aptr = Bptr = Cptr = NULL;//这句可要可不要,只是一个习惯,即delete掉的指针要置空,以防非法操作。
}
void insertlist (sequenlist *lstptr,int x)//istpter——>lstptr
{
int i=0,j=0;
//没有do....until ();应用while
while(ilast)
{
if (x<=lstptr->data[i])
break;
else i++;
}
if (lstptr->last>=maxsize)
{
printf ("overflow");
exit(0);//exit (overflow);overflow不是一个整数值
}
for (j=lstptr->last-1;j>=i;j--)//注意,j=lstptr->last-1
{
lstptr->data[j+1]=lstptr->data[j];
// lstptr->data[i]=x;
// lstptr->last++;
//这两句要放for外面
}
lstptr->data[i]=x;
lstptr->last++;
}
sequenlist *combine (sequenlist *scrptr,sequenlist *sinkptr)
{
//int x;不需要
int j;
for (j=0;jlast;j++)
insertlist (scrptr,sinkptr->data[j]);
return scrptr;//注意
}
全部回答
- 1楼网友:孤独的牧羊人
- 2021-01-28 21:10
如果2个程序没关联,好合并
#include
#include
int main1(void)
{
uint16_t count;
uint16_t timeLeft,timeRight;
usta_Init();
while(1)
{
DDRC|=_BV(PC0);
PORTC|=_BV(PC0);
delay_nms(2);
TCNT1=0;
DDRC&=~_BV(PC0);
TCCR1B=_BV(CS10);
while((PINC&0x01)!=0);
count=TCNT1;
timeLeft=count/4;
DDRC|=_BV(PC1);
PORTC|=_BV(PC1);
delay_nms(2);
TCNT1=0;
DDRC&=~_BV(PC1);
TCCR1B=_BV(CS10);
while((PINC&0x02)!=0);
count=TCNT1;
timeRight=count/4;
printf("timeLeft = %u us ",timeLeft);
printf("timeRight = %u us\n",timeRight);
delay_nms(100);
}
return 0;
}
int PC0_state(void)
{
return (PINC&0x01)?1:0;
}
int main2(void)
{
usta_Init();
printf("Program Running!");
DDRD|=_BV(PD7);
DDRC|=_BV(PC2)|_BV(PC3);
for(uint16_t counter=1;counter<=600;counter++)//开始/复位信号
{
PORTD|=_BV(PD7);
delay_nus(166);
PORTD&=~_BV(PD7);
delay_nus(166);
}
while(1)
{
if(PC0_state()==0)
{
PORTD|=_BV(PD7);
delay_nus(1000);
PORTD&=~_BV(PD7);
delay_nus(1000);
}
}
return 0;
}
int main(void)
{
int i;
do
{
printf("\r\n\r\n");
printf("1 操作功能1\r\n");
printf("2 操作功能2\r\n");
printf("0 退出\r\n");
printf("Please select operate:");
scanf("%d",&i);
if(i==1)
{
main1();
}
else if(i==2)
{
main2();
}
else if(i==0)
{
return 0;
}
}
return 0;
}
否则就不会了
- 2楼网友:白昼之月
- 2021-01-28 20:37
楼主是用秦志强的那本书吧,我在做书上的实验时,出现错误如下,有没有哪位大神指导一下
主程序:
#include
#include
int main(void)
{
usta_Init();
printf("Hello,this is a message from your Robot.");
}
头文件usta.h如下:
#include
#include
#include
#include
char g_aString[81];
int usart_putchar(char c)
{
if(c=='\n')
usart_putchar('\r');
loop_until_bit_is_set(UCSRA,UDRE);
UDR=c;
return 0;
}
int usart_getchar(void)
{
loop_until_bit_is_set(UCSRA,RXC);
return UDR;
}
void usta_Init(void)
{
UCSRB=_BV(RXEN)|_BV(TXEN);
UBRRL=25;
fdevopen(usart_putchar,usart_getchar,0);
}
MAKE ALL 后
avr-gcc (GCC) 3.4.3
Copyright (C) 2004 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Compiling: HelloRobot.c
avr-gcc -c -mmcu=atmega128 -I. -gdwarf-2 -DF_CPU=8000000UL -Os -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wall -Wstrict-prototypes -Wa,-adhlns=HelloRobot.lst -std=gnu99 -MD -MP -MF .dep/HelloRobot.o.d HelloRobot.c -o HelloRobot.o
In file included from HelloRobot.c:2:
D:/SOFTWARE/winavr/bin/../lib/gcc/avr/3.4.3/../../../../avr/include/avr/usta.h: In function `usart_putchar':
D:/SOFTWARE/winavr/bin/../lib/gcc/avr/3.4.3/../../../../avr/include/avr/usta.h:10: error: `UCSRA' undeclared (first use in this function)
D:/SOFTWARE/winavr/bin/../lib/gcc/avr/3.4.3/../../../../avr/include/avr/usta.h:10: error: (Each undeclared identifier is reported only once
D:/SOFTWARE/winavr/bin/../lib/gcc/avr/3.4.3/../../../../avr/include/avr/usta.h:10: error: for each function it appears in.)
D:/SOFTWARE/winavr/bin/../lib/gcc/avr/3.4.3/../../../../avr/include/avr/usta.h:11: error: `UDR' undeclared (first use in this function)
D:/SOFTWARE/winavr/bin/../lib/gcc/avr/3.4.3/../../../../avr/include/avr/usta.h: In function `usart_getchar':
D:/SOFTWARE/winavr/bin/../lib/gcc/avr/3.4.3/../../../../avr/include/avr/usta.h:16: error: `UCSRA' undeclared (first use in this function)
D:/SOFTWARE/winavr/bin/../lib/gcc/avr/3.4.3/../../../../avr/include/avr/usta.h:17: error: `UDR' undeclared (first use in this function)
D:/SOFTWARE/winavr/bin/../lib/gcc/avr/3.4.3/../../../../avr/include/avr/usta.h: In function `usta_Init':
D:/SOFTWARE/winavr/bin/../lib/gcc/avr/3.4.3/../../../../avr/include/avr/usta.h:21: error: `UCSRB' undeclared (first use in this function)
D:/SOFTWARE/winavr/bin/../lib/gcc/avr/3.4.3/../../../../avr/include/avr/usta.h:22: error: `UBRRL' undeclared (first use in this function)
HelloRobot.c:7:4: warning: no newline at end of file
make.exe: *** [HelloRobot.o] Error 1
> Process Exit Code: 2
我要举报
大家都在看
推荐资讯